package com.ztc.ZtcClassesForSale.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aqgj.Attachment.bean.Attachment;
import com.aqgj.User.bean.User;
import com.ztc.ZtcCar.bean.ZtcCar;
import com.ztc.ZtcCar.service.ZtcCarManager;
import com.ztc.ZtcClasses.bean.ZtcClasses;
import com.ztc.ZtcClasses.service.ZtcClassesManager;
import com.ztc.ZtcClassesForSale.bean.ZtcClassesForSale;
import com.ztc.ZtcClassesForSale.service.ZtcClassesForSaleManager;
import com.ztc.ZtcClassesPrice.bean.ZtcClassesPrice;
import com.ztc.ZtcClassesPriceForSale.bean.ZtcClassesPriceForSale;
import com.ztc.ZtcClassesPriceForSale.service.ZtcClassesPriceForSaleManager;
import com.ztc.ZtcClassesSite.bean.ZtcClassesSite;
import com.ztc.ZtcClassesSiteForSale.bean.ZtcClassesSiteForSale;
import com.ztc.ZtcClassesSiteForSale.service.ZtcClassesSiteForSaleManager;
import com.ztc.ZtcDriver.bean.ZtcDriver;
import com.ztc.ZtcDriver.service.ZtcDriverManager;
import com.ztc.ZtcOrder.bean.ZtcOrder;
import com.ztc.ZtcOrder.service.ZtcOrderManager;
import com.ztc.ZtcOrderTicket.bean.ZtcOrderTicket;
import com.ztc.ZtcOrderTicket.service.ZtcOrderTicketManager;
import com.ztc.ZtcRouteAttachment.bean.ZtcRouteAttachment;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("ztc_classes_for_sale_action")
public class ZtcClassesForSaleAction {
	@Autowired
	private ZtcClassesForSaleManager ztcClassesForSaleManager;
	@Autowired
	private ZtcClassesPriceForSaleManager ztcClassesPriceForSaleManager;
	@Autowired
	private ZtcClassesSiteForSaleManager ztcClassesSiteForSaleManager;
	@Autowired
	private ZtcOrderManager ztcOrderManager;
	@Autowired
	private ZtcOrderTicketManager ztcOrderTicketManager;
	@Autowired
	private ZtcDriverManager ztcDriverManager;
	@Autowired
	private ZtcCarManager ztcCarManager;
	@Autowired
	private ZtcClassesManager ztcClassesManager;
	@Autowired
	private ZtcTransportManager ztcTransportManager;
	@Autowired
	private ZtcOrderManager orderManager;
	@Autowired
	private ZtcOrderTicketManager orderTicketManager;
	private static final Logger log = LogManager.getLogger(ZtcClassesForSale.class);
	private static final SimpleDateFormat DATE_FORMET = new SimpleDateFormat("yyyy-MM-dd");
	private static final SimpleDateFormat TIME_FORMET = new SimpleDateFormat("HH:mm");
	private static final SimpleDateFormat DATE_TIME_FORMET = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static final float childSeatRatio = 0.1f;

	/**
	 *
	 *停售在线班次
	 * @param id 在线班次id
	 * @param session
	 * @return 操作结果信息
	 * @throws ParseException
	 */
	@RequestMapping("stopSell")
	@ResponseBody
	public HashMap<String, Object> stopSell(String id, HttpSession session) throws ParseException {
		HashMap<String, Object> wapper = new HashMap<>();
		Date now=new Date();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
		int transportType = transport.getType();
		if (transportType == 0 || transportType == 1 || transportType == 2) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "非运营公司或车方不可操作在售班次");
			log.info("非运营公司或车方不可操作在售班次");
			return wapper;
		}
		if (transport.getStatus() == 4) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该用户公司已被冻结，不可停售在售班次");
			log.info("该用户公司已被冻结，不可停售在售班次");
			return wapper;
		}
		ZtcClassesForSale ztcClassesForSale = new ZtcClassesForSale();
		ztcClassesForSale.setId(id);
		ZtcClassesForSale ztcClassesForSaleBean = ztcClassesForSaleManager.searchInfoEquals(ztcClassesForSale).get(0);
		if (ztcClassesForSaleBean.getTicketStatus() == 1) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该班次已停售");
			return wapper;
		}
		ztcClassesForSaleBean.setModifyTime(new Date());
		ztcClassesForSaleBean.setFkModifyUserId(user == null ? "" : user.getId());
		String result = ztcClassesForSaleManager.stopSell(ztcClassesForSaleBean,user,now);
		if ("0".equals(result)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "网络错误");
			return wapper;
		}
		ZtcClasses ztcClasses=new ZtcClasses();
		ztcClasses.setId(ztcClassesForSaleBean.getFkZtcClassesId());
		ZtcClasses classes = ztcClassesManager.searchInfoEquals(ztcClasses).get(0);
		ZtcClassesForSale ztcClassesForSaleNew=new ZtcClassesForSale();
		ztcClassesForSaleNew.setFkZtcClassesId(classes.getId());
		Date startDate = classes.getDepartureDate();
		if(now.getTime()>startDate.getTime()){
			startDate=now;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(startDate);
		calendar.add(Calendar.DATE, 35);
		Date endDate = calendar.getTime();
		List<ZtcClassesForSale> list = ztcClassesForSaleManager.searchInfoWithDate(ztcClassesForSaleNew,
				DATE_FORMET.format(startDate), DATE_FORMET.format(endDate));
		String stringList = generateNewList(list, classes);
		JSONArray jsonList = JSON.parseArray(stringList);
		wapper.put("list", jsonList);
		//wapper.put("state", "2");
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", "停售成功");
		return wapper;
	}

	/**
	 * 起售在线班次
	 * @param id 在线班次id
	 * @param classesId 班次模板id
	 * @param departureDate 发车日期
	 * @param session
	 * @return 操作结果信息
	 * @throws ParseException
	 */
	@RequestMapping("restartToSell")
	@ResponseBody
	public HashMap<String, Object> restartToSell(String id,String classesId, String departureDate,HttpSession session) throws ParseException {
		HashMap<String, Object> wapper = new HashMap<>();
		Date now=new Date();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
		int transportType = transport.getType();
		if (transportType == 0 || transportType == 1 || transportType == 2) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "非运营公司或车方不可操作在售班次");
			log.info("非运营公司或车方不可操作在售班次");
			return wapper;
		}
		if (transport.getStatus() == 4) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该用户公司已被冻结，不可起售停售班次");
			log.info("该用户公司已被冻结，不可起售停售班次");
			return wapper;
		}
		if(id!=null&&!"".equals(id)){
			ZtcClassesForSale ztcClassesForSale = new ZtcClassesForSale();
			ztcClassesForSale.setId(id);
			ZtcClassesForSale ztcClassesForSaleBean = ztcClassesForSaleManager.searchInfoEquals(ztcClassesForSale).get(0);
			if (ztcClassesForSaleBean.getTicketStatus() == 0) {
				wapper.put("success", Boolean.valueOf(false));
				wapper.put("msg", "该班次已起售");
				return wapper;
			}
			ztcClassesForSaleBean.setModifyTime(now);
			ztcClassesForSaleBean.setFkModifyUserId(user == null ? "" : user.getId());
			String result = ztcClassesForSaleManager.restartToSell(ztcClassesForSaleBean,user,now);
			if ("0".equals(result)) {
				wapper.put("success", Boolean.valueOf(false));
				wapper.put("msg", "网络错误");
				return wapper;
			}
		}else{
			ZtcClasses ztcClasses = new ZtcClasses();
			ztcClasses.setId(classesId);
			ZtcClasses ztcClassesBean = ztcClassesManager.searchInfoEquals(ztcClasses).get(0);
			List<ZtcClassesSite> classesSites = ztcClassesManager.getSites(ztcClassesBean);
			List<ZtcClassesPrice> classesPrices = ztcClassesManager.getPrices(ztcClassesBean);
			String data = generateDataForReSell(classesSites, classesPrices, ztcClassesBean);
			JSONArray dataArray = JSON.parseArray(data);
			JSONArray siteArray = new JSONArray();
			JSONArray priceArray = new JSONArray();
			ZtcClassesForSale classesForSale = new ZtcClassesForSale();
			classesForSale.setName(ztcClassesBean.getName());
			classesForSale.setRemark(ztcClassesBean.getRemark());
			classesForSale.setType(0);
			classesForSale.setStatus(0);
			classesForSale.setTicketStatus(0);
			classesForSale.setCarType(ztcClassesBean.getCarType());
			classesForSale.setIsRefund(ztcClassesBean.getIsRefund());
			classesForSale.setDepartureDate(DATE_FORMET.parse(departureDate));
			classesForSale.setForbidSaleTimeRegionBeforeDeparture(ztcClassesBean.getForbidSaleTimeRegionBeforeDeparture());
			classesForSale.setAddTime(new Date());
			classesForSale.setFkAddUserId(user.getId());
			classesForSale.setFkZtcCarId(ztcClassesBean.getFkZtcCarId());
			classesForSale.setFkZtcDriverId(ztcClassesBean.getFkZtcDriverId());
			classesForSale.setFkZtcClassesId(classesId);
			classesForSale.setChildSeatNumber(ztcClassesBean.getChildSeatNumber());
			classesForSale.setSeatNumber(ztcClassesBean.getSeatNumber());
			classesForSale.setFkTransportId(ztcClassesBean.getFkTransportId());
			getSiteAndPriceWithSeatInfoArray(dataArray, siteArray, priceArray, ztcClassesBean.getSeatNumber());
			String addId = generateClassesForSale(classesForSale, siteArray, priceArray);
			if (addId == null) {
				wapper.put("success", Boolean.valueOf(false));
				wapper.put("msg", "网络错误");
				log.info("网络错误");
				return wapper;
			}
		}
		ZtcClasses ztcClasses=new ZtcClasses();
		ztcClasses.setId(classesId);
		ZtcClasses classes = ztcClassesManager.searchInfoEquals(ztcClasses).get(0);
		ZtcClassesForSale ztcClassesForSale=new ZtcClassesForSale();
		ztcClassesForSale.setFkZtcClassesId(classesId);
		Date startDate = classes.getDepartureDate();
		if(now.getTime()>startDate.getTime()){
			startDate=now;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(startDate);
		calendar.add(Calendar.DATE, 35);
		Date endDate = calendar.getTime();
		List<ZtcClassesForSale> list = ztcClassesForSaleManager.searchInfoWithDate(ztcClassesForSale,
				DATE_FORMET.format(startDate), DATE_FORMET.format(endDate));
		String stringList = generateNewList(list, classes);
		JSONArray jsonList = JSON.parseArray(stringList);
		wapper.put("list", jsonList);
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", "起售成功");
		return wapper;
	}
	/**
	 * 生成在线班次及其站点和价格
	 * @param classesForSale 在线班次对象
	 * @param siteArray 在线班次站点集合
	 * @param priceArray 在线班次价格集合
	 * @return 操作结果信息：null.失败，非null.成功
	 * @throws ParseException
	 */
	protected String generateClassesForSale(ZtcClassesForSale classesForSale, JSONArray siteArray, JSONArray priceArray) throws ParseException {
		String id = ztcClassesForSaleManager.save(classesForSale);
		Date siteDepartureDate = classesForSale.getDepartureDate();
		if (id != null) {
			// 获取并创建站点对象
			Map<Integer, ZtcClassesSiteForSale> siteMap = new HashMap<>();
			for (int i = 0; i < siteArray.size(); i++) {
				JSONObject object = siteArray.getJSONObject(i);
				if (object == null || object.isEmpty()) {
					continue;
				}
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(classesForSale.getDepartureDate());
				calendar.add(Calendar.DATE, Integer.parseInt(object.getString("daysAfterDepartureDate")));
				ZtcClassesSiteForSale site = new ZtcClassesSiteForSale();
				String siteId = buildClassesSiteForSale(id, object,
						DATE_FORMET.format(calendar.getTime()) + " " + object.getString("departureTime"));
				if (siteId != null && !"".equals(siteId)) {
					site.setId(siteId);
					siteMap.put(object.getInteger("row"), site);
				} else {
					return null;
				}
			}
			for (int i = 0; i < priceArray.size(); i++) {
				JSONObject object = priceArray.getJSONObject(i);
				if (object == null || object.isEmpty()) {
					continue;
				}
				String priceId = buildClassesPriceForSale(id, object, siteMap, classesForSale);
				if (priceId == null) {
					return null;
				}
			}
		}
		return id;
	}

	/**
	 * 生成在线班次站点
	 * @param id 在线班次id
	 * @param object 包含在线班次站点信息的对象
	 * @param siteForSaleDepartureTime 在线班次站点发车时间
	 * @return 操作结果信息：null.失败，非null.成功
	 * @throws ParseException
	 */
	private String buildClassesSiteForSale(String id, JSONObject object, String siteForSaleDepartureTime)
			throws ParseException {
		ZtcClassesSiteForSale ztcSiteForSale = new ZtcClassesSiteForSale();
		ztcSiteForSale.setName(object.getString("name"));
		ztcSiteForSale.setManagerPhone(object.getString("managerPhone"));
		ztcSiteForSale.setAreaCity(object.getString("areaCity"));
		ztcSiteForSale.setAreaCode(object.getString("areaCode"));
		ztcSiteForSale.setLat(object.getString("lat"));
		ztcSiteForSale.setStatus(0);
		ztcSiteForSale.setSequence(Integer.parseInt(object.getString("row")));
		ztcSiteForSale.setDepartureTime(DATE_TIME_FORMET.parse(siteForSaleDepartureTime));
		ztcSiteForSale.setFkZtcClassesForSaleId(id);
		String siteForSaleId = ztcClassesSiteForSaleManager.save(ztcSiteForSale);
		return siteForSaleId;
	}

	/**
	 * 生成在线班次价格
	 * @param id 在线班次id
	 * @param object 包含在线班次价格信息的对象
	 * @param siteForSaleMap 在线班次站点集合
	 * @param classesForSale 在线班次对象
	 * @return 操作结果信息：null.失败，非null.成功
	 */
	private String buildClassesPriceForSale(String id, JSONObject object,
											Map<Integer, ZtcClassesSiteForSale> siteForSaleMap, ZtcClassesForSale classesForSale) {
		ZtcClassesSiteForSale start = siteForSaleMap.get(object.getInteger("col"));
		if (start == null) {
			return null;
		}
		// 设置到站点
		ZtcClassesSiteForSale end = siteForSaleMap.get(object.getInteger("row"));
		if (end == null) {
			return null;
		}
		String commonPrice = object.getString("price");
		ZtcClassesPriceForSale ztcPriceForSale = new ZtcClassesPriceForSale();
		ztcPriceForSale.setFkStartClassesSiteForSaleId(start.getId());
		ztcPriceForSale.setFkEndClassesSiteForSaleId(end.getId());
		ztcPriceForSale.setPrice(commonPrice);
		ztcPriceForSale.setDistance(object.getString("distance"));
		ztcPriceForSale.setStatus(0);
		ztcPriceForSale.setRemainChildSeatNumber(classesForSale.getChildSeatNumber());
		ztcPriceForSale.setRemainSeatNumber(classesForSale.getSeatNumber());
		String info = "";
		for (int i = 0; i < classesForSale.getSeatNumber(); i++) {
			info += "(" + (i+1) + ",0," +commonPrice + ",0),";
		}
		if (!"".equals(info)) {
			info = info.substring(0, info.length() - 1);
			ztcPriceForSale.setSeatInfo(info);
		}
		ztcPriceForSale.setSeatInfo(info);
		ztcPriceForSale.setFkClassesForSaleId(id);
		String priceForSaleId = ztcClassesPriceForSaleManager.save(ztcPriceForSale);
		return priceForSaleId;
	}

	/**
	 * 根据梯形表数据，座位数，生成站点集合，价格集合
	 * @param dataArray 梯形表数据
	 * @param siteArray 站点集合
	 * @param priceArray 价格集合
	 * @param seatNumber 座位数
	 */
	private void getSiteAndPriceWithSeatInfoArray(JSONArray dataArray, JSONArray siteArray, JSONArray priceArray,
												  int seatNumber) {
		siteArray.clear();
		priceArray.clear();
		List<Integer> commonSeatNumbers =new ArrayList<Integer>();
		for(int i=0;i<seatNumber;i++){
			commonSeatNumbers.add(i+1);
		}
		if (dataArray != null) {
			for (int i = 0; i < dataArray.size(); i++) {
				JSONObject siteObject = dataArray.getJSONObject(i);
				String siteId = siteObject.getString("id");
				String siteName = siteObject.getString("name");
				String siteDetail = siteObject.getString("detailed");
				String siteLat = siteObject.getString("lat");
				String managerPhone = siteObject.getString("managerPhone");
				String siteDepartureTime = siteObject.getString("departureTime");
				String daysAfterDepartureDate = siteObject.getString("daysAfterDepartureDate");
				String siteString = "{\"id\":\"" + siteId + "\",\"name\":\"" + siteDetail + "\",\"areaCity\":\""
						+ siteName + "\",\"managerPhone\":\"" + managerPhone + "\",\"lat\":\"" + siteLat
						+ "\",\"departureTime\":\"" + siteDepartureTime + "\",\"daysAfterDepartureDate\":\"" + daysAfterDepartureDate  + "\",\"row\":\"" + i + "\"}";
				siteArray.add(JSON.parseObject(siteString));
				JSONArray price = siteObject.getJSONArray("cell");
				if (price != null) {
					for (int j = 0; j < price.size(); j++) {
						JSONObject object = price.getJSONObject(j);
						String priceId = object.getString("id");
						String priceValue = object.getString("pirce");
						String distance = object.getString("distance");
						if (priceValue != null && distance != null && !"".equals(priceValue) && !"".equals(distance)) {
							String priceString = "{\"id\":\"" + priceId + "\",\"price\":\"" + priceValue
									+ "\",\"distance\":\"" + distance + "\",\"seatInfo\":\""
									+ getSeatInfo(commonSeatNumbers, priceValue) + "\",\"row\":\"" + i + "\",\"col\":\""
									+ j + "\"}";
							JSONObject priceObject = JSON.parseObject(priceString);
							priceArray.add(priceObject);
						}
					}
				}
			}
		}
	}

	/**
	 * 根据普通座位号集合，价格生成座位号信息
	 * @param commonSeatNumbers 普通座位号集合
	 * @param priceValue 价格
	 * @return 座位号信息
	 */
	private String getSeatInfo(List<Integer> commonSeatNumbers, String priceValue) {
		String seatInfo = "";
		for (int i = 0; i < commonSeatNumbers.size(); i++) {
			seatInfo += "(" + commonSeatNumbers.get(i) + ",0," + priceValue + ",0),";
		}
		if (seatInfo.length() > 0) {
			seatInfo = seatInfo.substring(0, seatInfo.length() - 1);
		}
		return seatInfo;
	}


	/**
	 *根据站点集合，价格集合，班次模板生成用于起售在线班次的梯形表数据
	 * @param classesSites 站点集合
	 * @param classesPrices 价格集合
	 * @param ztcClassesBean 班次模板
	 * @return 用于起售在线班次的梯形表数据
	 */
	private String generateDataForReSell(List<ZtcClassesSite> classesSites, List<ZtcClassesPrice> classesPrices,
										 ZtcClasses ztcClassesBean) {
		String data = "[";
		List<ZtcClassesSite> startZtcClassesSites = new ArrayList<ZtcClassesSite>();
		String startName = "";
		for (int i = 0; i < classesSites.size(); i++) {
			ZtcClassesSite endZctClassesSite = classesSites.get(i);
			if (i == 0) {
				startName = (endZctClassesSite.getAreaCity() == null ? "" : endZctClassesSite.getAreaCity());
			}
			String cell = "[";
			for (int j = 0; j < startZtcClassesSites.size(); j++) {
				ZtcClassesSite startZtcClassesSite = startZtcClassesSites.get(j);
				if (classesSites.size() == 0) {
					cell += "{\"pirce\":\"\",\"distance\":\"\",\"seat\":[]}";
					if (j != startZtcClassesSites.size() - 1) {
						cell += ",";
					}
				} else {
					boolean findResult = false;
					int index = 0;
					for (int k = 0; k < classesPrices.size(); k++) {
						ZtcClassesPrice classesPrice = classesPrices.get(k);
						if (startZtcClassesSite.getId().equals(classesPrice.getFkStartClassesSiteId())
								&& endZctClassesSite.getId().equals(classesPrice.getFkEndClassesSiteId())) {
							index = k;
							cell += "{\"pirce\":\"" + classesPrice.getPrice() + "\",\"id\":\"" + classesPrice.getId()
									+ "\",\"distance\":\"" + classesPrice.getDistance() + "\",\"startName\":\""
									+ startName + "\",\"seat\":"
									+ JSON.parseArray(generateSeatInfoForReSell(classesPrice, ztcClassesBean)) + "}";
							if (j != startZtcClassesSites.size() - 1) {
								cell += ",";
							}
							findResult = true;
							break;
						}
					}
					if (findResult) {
						classesPrices.remove(index);
					} else {
						cell += "{\"pirce\":\"\",\"distance\":\"\",\"seat\":[]}";
						if (j != startZtcClassesSites.size() - 1) {
							cell += ",";
						}
					}
				}
			}
			if (!"[".equals(cell)) {
				cell.substring(0, cell.length() - 1);
			}
			cell += "]";
			data = generateCellContentInfoForReSell(classesSites, data, i, endZctClassesSite, cell);
			startZtcClassesSites.add(endZctClassesSite);
		}
		if (!"[".equals(data)) {
			data.substring(0, data.length() - 1);
		}
		data += "]";
		return data;
	}

	/**
	 * 根据价格，班次模板生成座位信息
	 * @param classesPrice 价格
	 * @param ztcClassesBean 班次模板
	 * @return 座位信息
	 */
	private String generateSeatInfoForReSell(ZtcClassesPrice classesPrice, ZtcClasses ztcClassesBean) {
		int seatNumber = ztcClassesBean.getSeatNumber();
		String info = "[";
		for (int i = 0; i < seatNumber; i++) {
			info += "{" + "'pirce':'" + classesPrice.getPrice() + "','seatnumber':'" + (i + 1)
					+ "','state':'0','checkTrue':false},";
		}
		info = info.substring(0, info.length() - 1);
		info += "]";
		return info;
	}

	/**
	 * 根据站点集合，当前站点下标，结束站点，价格信息 生成一行梯形表数据
	 * @param classesSites 站点集合
	 * @param data 用于装载一行梯形表数据
	 * @param i 当前站点下标
	 * @param endZctClassesSite 结束站点
	 * @param cell 价格信息
	 * @return 一行梯形表数据
	 */
	private String generateCellContentInfoForReSell(List<ZtcClassesSite> classesSites, String data, int i,
													ZtcClassesSite endZctClassesSite, String cell) {
		if (i == 0) {
			data += "{\"name\":\"" + (endZctClassesSite.getAreaCity() == null ? "" : endZctClassesSite.getAreaCity())
					+ "\",\"id\":\"" + endZctClassesSite.getId() + "\",\"managerPhone\":\""
					+ endZctClassesSite.getManagerPhone() + "\",\"lat\":\"" + endZctClassesSite.getLat()
					+ "\",\"row\":\"" + endZctClassesSite.getSequence()
					+ "\",\"departureTime\":\"" + (endZctClassesSite.getDepartureTime() == null ? ""
					: TIME_FORMET.format(endZctClassesSite.getDepartureTime()))
					+ "\",\"daysAfterDepartureDate\":\"" + endZctClassesSite.getDaysAfterDepartureDate()
					+ "\",\"remark\":\"起点\",\"detailed\":\""
					+ (endZctClassesSite.getName() == null ? "" : endZctClassesSite.getName()) + "\"},";
		} else if (i == classesSites.size() - 1) {
			data += "{\"name\":\"" + (endZctClassesSite.getAreaCity() == null ? "" : endZctClassesSite.getAreaCity())
					+ "\",\"id\":\"" + endZctClassesSite.getId() + "\",\"managerPhone\":\""
					+ endZctClassesSite.getManagerPhone() + "\",\"lat\":\"" + endZctClassesSite.getLat()
					+ "\",\"row\":\"" + endZctClassesSite.getSequence()
					+ "\",\"departureTime\":\"" + (endZctClassesSite.getDepartureTime() == null ? ""
					: TIME_FORMET.format(endZctClassesSite.getDepartureTime()))
					+ "\",\"daysAfterDepartureDate\":\"" + endZctClassesSite.getDaysAfterDepartureDate()
					+ "\",\"remark\":\"终点\",\"detailed\":\""
					+ (endZctClassesSite.getName() == null ? "" : endZctClassesSite.getName()) + "\",\"cell\":" + cell
					+ "},";
		} else {
			data += "{\"name\":\"" + (endZctClassesSite.getAreaCity() == null ? "" : endZctClassesSite.getAreaCity())
					+ "\",\"id\":\"" + endZctClassesSite.getId() + "\",\"managerPhone\":\""
					+ endZctClassesSite.getManagerPhone() + "\",\"lat\":\"" + endZctClassesSite.getLat()
					+ "\",\"row\":\"" + endZctClassesSite.getSequence()
					+ "\",\"departureTime\":\""  + (endZctClassesSite.getDepartureTime() == null ? ""
					: TIME_FORMET.format(endZctClassesSite.getDepartureTime()))
					+ "\",\"daysAfterDepartureDate\":\"" + endZctClassesSite.getDaysAfterDepartureDate()
					+ "\",\"detailed\":\"" + (endZctClassesSite.getName() == null ? "" : endZctClassesSite.getName())
					+ "\",\"cell\":" + cell + "},";
		}
		return data;
	}

	/**
	 * 修改在线班次
	 * @param id 在线班次id
	 * @param session
	 * @param data 梯形表数据
	 * @param carType 车型
	 * @param carId 车辆id
	 * @param isRefund 是否可退票
	 * @param fsTimerbDeparture 发车前多长时间停止售票
	 * @param driverId 司机id
	 * @param childSeatNumber 儿童座位数
	 * @return 操作结果信息
	 * @throws ParseException
	 */
	@RequestMapping("update")
	@ResponseBody
	public HashMap<String, Object> update(String id, HttpSession session, String data, String carType, String carId,
										  String isRefund, String fsTimerbDeparture, String driverId, String childSeatNumber) throws ParseException {
		HashMap<String, Object> wapper = new HashMap<>();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
		int transportType = transport.getType();
		if (transportType == 0 || transportType == 1 || transportType == 2) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "非运营公司或车方不可操作在售班次");
			log.info("非运营公司或车方不可操作在售班次");
			return wapper;
		}
		if (transport.getStatus() == 4) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该用户公司已被冻结，不可修改在售班次");
			log.info("该用户公司已被冻结，不可修改在售班次");
			return wapper;
		}
		LinkedHashMap<String, String> argumentsAndRoles = new LinkedHashMap<>();
		if ("".equals(carId) || carId == null) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "请选择车辆");
			return wapper;
		}
		if ("".equals(driverId) || driverId == null) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "请选择司机");
			return wapper;
		}
		if ("".equals(fsTimerbDeparture) || fsTimerbDeparture == null) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "请填写发车前暂停售票的时间范围");
			return wapper;
		}
		argumentsAndRoles.put(fsTimerbDeparture + "#发车前暂停售票的时间范围格式如'1.01'",
				"^(([1-9][0-9]{0,9})|([0])|(([0-9].[0-9]{1,2})|([1-9][0-9]{1,7}.[0-9]{1})|([1-9][0-9]{1,6}.[0-9]{2})))$");
		if ("".equals(childSeatNumber) || childSeatNumber == null) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "请填写儿童座数");
			return wapper;
		}
		argumentsAndRoles.put(childSeatNumber + "#儿童票格式如'0'最大不超过6", "^[0-6]$");
		examineArgument(argumentsAndRoles, wapper);
		if ("false".equals(String.valueOf(wapper.get("success")))) {
			return wapper;
		}
		examineSeatInfo(data, wapper);
		if ("false".equals(String.valueOf(wapper.get("success")))) {
			return wapper;
		}
		JSONArray dataArray = JSON.parseArray(data);
		JSONArray siteArray = new JSONArray();
		JSONArray priceArray = new JSONArray();
		getSiteAndPriceArray(dataArray, siteArray, priceArray);
		ZtcClassesForSale ztcClassesForSale = new ZtcClassesForSale();
		ztcClassesForSale.setId(id);
		ZtcClassesForSale ztcClassesForSaleBean = ztcClassesForSaleManager.searchInfoEquals(ztcClassesForSale).get(0);
		ZtcClasses ztcClasses = new ZtcClasses();
		ztcClasses.setId(ztcClassesForSaleBean.getFkZtcClassesId());
		ZtcClasses ztcClassesBean = ztcClassesManager.searchInfoEquals(ztcClasses).get(0);
		if (ztcClassesForSaleBean.getTicketStatus() == 1) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该在售班次已停售");
			log.info("该在售班次已停售");
			return wapper;
		}
		ztcClassesForSaleBean.setFkModifyUserId(user == null ? "" : user.getId());
		ztcClassesForSaleBean.setCarType(Integer.parseInt(carType));
		ztcClassesForSaleBean.setFkZtcCarId(carId);
		ztcClassesForSaleBean.setIsRefund("true".equals(isRefund) ? 1 : 0);
		ztcClassesForSaleBean.setModifyTime(new Date());
		ztcClassesForSaleBean.setForbidSaleTimeRegionBeforeDeparture(fsTimerbDeparture);
		int maxChildSeatNumber = (int) (ztcClassesBean.getSeatNumber() * childSeatRatio);
		if (maxChildSeatNumber < Integer.parseInt(childSeatNumber)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "儿童票数量太多，应不超过" + maxChildSeatNumber + "");
			log.info("儿童票数量太多，应不超过" + maxChildSeatNumber + "");
			return wapper;
		}
		ztcClassesForSaleBean.setChildSeatNumber(Integer.parseInt(childSeatNumber));
		ztcClassesForSaleBean.setFkZtcDriverId(driverId);
		String ztcClassesBeanId = ztcClassesForSaleManager.update(ztcClassesForSaleBean);
		if ("0".equals(ztcClassesBeanId)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "网络错误");
			log.info("网络错误");
			return wapper;
		}
		ZtcClassesPriceForSale ztcClassesPriceForSale = new ZtcClassesPriceForSale();
		ztcClassesPriceForSale.setFkClassesForSaleId(id);
		List<ZtcClassesPriceForSale> ztcClassesPriceForSales = ztcClassesPriceForSaleManager
				.searchInfoEquals(ztcClassesPriceForSale);
		for (int i = 0; i < ztcClassesPriceForSales.size(); i++) {
			ZtcClassesPriceForSale classesPriceForSale = ztcClassesPriceForSales.get(i);
			if (classesPriceForSale.getStatus() == 1) {
				continue;
			}
			ZtcClassesSiteForSale site = new ZtcClassesSiteForSale();
			site.setId(classesPriceForSale.getFkStartClassesSiteForSaleId());
			ZtcClassesSiteForSale startSiteForSale = ztcClassesSiteForSaleManager.searchInfoEquals(site).get(0);
			site.setId(classesPriceForSale.getFkEndClassesSiteForSaleId());
			ZtcClassesSiteForSale endSiteForSale = ztcClassesSiteForSaleManager.searchInfoEquals(site).get(0);
			int col = startSiteForSale.getSequence();
			int row = endSiteForSale.getSequence();
			for (int j = 0; j < priceArray.size(); j++) {
				JSONObject objectPrice = priceArray.getJSONObject(j);
				if (objectPrice.getInteger("row") == row && objectPrice.getInteger("col") == col) {
					classesPriceForSale.setPrice(objectPrice.getString("price"));
					classesPriceForSale.setDistance(objectPrice.getString("distance"));
					// 座位信息设置
					String modifySeatInfo=modifySeatInfo(objectPrice, classesPriceForSale, childSeatNumber);
					if(!"".equals(modifySeatInfo)&&!"()".equals(modifySeatInfo)){
						classesPriceForSale.setSeatInfo(modifySeatInfo);
					}
					//
					String updateResult = ztcClassesPriceForSaleManager.update(classesPriceForSale);
					if ("0".equals(updateResult)) {
						wapper.put("success", Boolean.valueOf(false));
						wapper.put("msg", "网络错误");
						log.info("网络错误");
						return wapper;
					}
				}
			}
		}
		/*ZtcClassesSiteForSale ztcClassesSiteForSale = new ZtcClassesSiteForSale();
		ztcClassesSiteForSale.setFkZtcClassesForSaleId(id);
		List<ZtcClassesSiteForSale> sites = ztcClassesSiteForSaleManager.searchInfoEquals(ztcClassesSiteForSale);
		for (int k = 0; k < sites.size(); k++) {
			ZtcClassesSiteForSale site = sites.get(k);
			for (int l = 0; l < siteArray.size(); l++) {
				JSONObject siteObject = siteArray.getJSONObject(l);
				if (site.getSequence() == siteObject.getInteger("row")) {
					site.setManagerPhone(siteObject.getString("managerPhone"));
					try {
						site.setDepartureTime(DATE_FORMET.parse(siteObject.getString("departureTime")));
					} catch (ParseException e) {
						e.printStackTrace();
					}
					String result = ztcClassesSiteForSaleManager.update(site);
					if ("0".equals(result)) {
						wapper.put("success", Boolean.valueOf(false));
						wapper.put("msg", "网络错误");
						log.info("网络错误");
						return wapper;
					} else {
						break;
					}
				}
			}
		}*/
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", "修改成功");
		return wapper;
	}

	/**
	 * 根据在线班次价格id，座位信息修改在线班次价格
	 * @param priceId 在线班次价格id
	 * @param session
	 * @param seat 座位信息
	 * @return 操作结果信息
	 * @throws ParseException
	 */
	@RequestMapping("updateForModify")
	@ResponseBody
	public HashMap<String, Object> updateForModify(String priceId, HttpSession session, String seat) {
		HashMap<String, Object> wapper = new HashMap<>();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
		int transportType = transport.getType();
		if (transportType == 0 || transportType == 1 || transportType == 2) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "非运营公司或车方不可操作在售班次");
			log.info("非运营公司或车方不可操作在售班次");
			return wapper;
		}
		if (transport.getStatus() == 4) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该用户公司已被冻结，不可修改在售班次");
			log.info("该用户公司已被冻结，不可修改在售班次");
			return wapper;
		}
		LinkedHashMap<String, String> argumentsAndRoles = new LinkedHashMap<String, String>();
		if (!"".equals(seat) && seat != null) {
			JSONArray seatInfos = JSONArray.parseArray(seat);
			for (int j = 0; j < seatInfos.size(); j++) {
				JSONObject seatInfo = seatInfos.getJSONObject(j);
				if (seatInfo == null) {
					continue;
				}
				String price = seatInfo.getString("pirce");
				if (!"".equals(price)) {
					argumentsAndRoles.clear();
					if (price.length() > 10) {
						wapper.put("success", Boolean.valueOf(false));
						wapper.put("msg", "价格的长度不可超过10位");
						return wapper;
					}
					argumentsAndRoles.put(price + "#价格的格式如'100.01'",
							"^(([1-9][0-9]{0,9})|([0-9])|(([0-9].[0-9]{1,2})|([1-9][0-9]{1,7}.[0-9]{1})|([1-9][0-9]{1,6}.[0-9]{2})))$");
					examineArgument(argumentsAndRoles, wapper);
				}
			}
		}
		if ("false".equals(String.valueOf(wapper.get("success")))) {
			return wapper;
		}
		ZtcClassesPriceForSale priceForSale=new ZtcClassesPriceForSale();
		priceForSale.setId(priceId);
		ZtcClassesPriceForSale classesPriceForSale =ztcClassesPriceForSaleManager.searchInfoEquals(priceForSale).get(0);
		String modifySeatInfo=generateSeatInfoForModify(seat, classesPriceForSale);
		if(!"".equals(modifySeatInfo)&&!"()".equals(modifySeatInfo)){
			classesPriceForSale.setSeatInfo(modifySeatInfo);
		}
		String updateResult = ztcClassesPriceForSaleManager.update(classesPriceForSale);
		if ("0".equals(updateResult)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "网络错误");
			log.info("网络错误");
			return wapper;
		}
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", "修改成功");
		return wapper;
	}

	/**
	 * 根据在线班次价格id，生成供修改的座位信息
	 * @param priceId 在线班次价格id
	 * @param session
	 * @return 供修改的座位信息
	 * @throws ParseException
	 */
	@RequestMapping("getSeatInfoForModify")
	@ResponseBody
	public HashMap<String, Object> getSeatInfoForModify(String priceId, HttpSession session){
		HashMap<String, Object> wapper = new HashMap<>();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
		int transportType = transport.getType();
		if (transportType == 0 || transportType == 1 || transportType == 2) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "非运营公司或车方不可操作在售班次");
			log.info("非运营公司或车方不可操作在售班次");
			return wapper;
		}
		if (transport.getStatus() == 4) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该用户公司已被冻结，不可修改在售班次");
			log.info("该用户公司已被冻结，不可修改在售班次");
			return wapper;
		}
		ZtcClassesPriceForSale priceForSale=new ZtcClassesPriceForSale();
		priceForSale.setId(priceId);
		ZtcClassesPriceForSale classesPriceForSale =ztcClassesPriceForSaleManager.searchInfoEquals(priceForSale).get(0);
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("seat",JSON.parseArray(generateSeatInfo(classesPriceForSale)));
		return wapper;
	}

	/**
	 * 根据座位信息修改在线班次价格，同时生成用于修改的在线班次座位信息
	 * @param priceSeat 座位信息
	 * @param ztcClassesPriceForSale 需要修改的在线班次价格
	 * @return  用于修改的在线班次座位信息
	 */
	private String generateSeatInfoForModify(String priceSeat, ZtcClassesPriceForSale ztcClassesPriceForSale) {
		JSONArray seatArray = JSONArray.parseArray(priceSeat);
		String seatInfo = ztcClassesPriceForSale.getSeatInfo();
		String[] seatInfos = seatInfo.split("\\u0029,");
		String modifySeatInfos = "";
		int commonOnSeatNumber = 0;
		int specialOnSeatNumber = 0;
		int highOnSeatNumber = 0;
		int commonSaleSeatNumber = 0;
		int specialSaleSeatNumber = 0;
		int highSaleSeatNumber = 0;
		HashMap<String, Object> result = modifySeatInfoAssistant(seatInfos, seatArray, modifySeatInfos,
				ztcClassesPriceForSale, commonOnSeatNumber, specialOnSeatNumber, highOnSeatNumber, commonSaleSeatNumber,
				specialSaleSeatNumber, highSaleSeatNumber);
		modifySeatInfos = String.valueOf(result.get("modifySeatInfos"));
		commonOnSeatNumber = Integer.parseInt(String.valueOf(result.get("commonOnSeatNumber")));
		specialOnSeatNumber = Integer.parseInt(String.valueOf(result.get("specialOnSeatNumber")));
		highOnSeatNumber = Integer.parseInt(String.valueOf(result.get("highOnSeatNumber")));
		commonSaleSeatNumber = Integer.parseInt(String.valueOf(result.get("commonSaleSeatNumber")));
		specialSaleSeatNumber = Integer.parseInt(String.valueOf(result.get("specialSaleSeatNumber")));
		highSaleSeatNumber = Integer.parseInt(String.valueOf(result.get("highSaleSeatNumber")));
		int remainSeatNumber = commonOnSeatNumber + specialOnSeatNumber + highOnSeatNumber;
		if (remainSeatNumber == 0) {
			ztcClassesPriceForSale.setStatus(1);
		}
		ztcClassesPriceForSale.setRemainSeatNumber(remainSeatNumber);
		int haveSaleChildSeatNumber = 0;
		ZtcOrder order = new ZtcOrder();
		order.setFkZtcClassesPriceForSaleId(ztcClassesPriceForSale.getId());
		List<ZtcOrder> orders = ztcOrderManager.searchInfo(order);
		for (int i = 0; i < orders.size(); i++) {
			ZtcOrder ztcOrder = orders.get(i);
			if (ztcOrder.getStatus() == 0 || ztcOrder.getStatus() == 1) {
				ZtcOrderTicket ztcOrderTicket = new ZtcOrderTicket();
				ztcOrderTicket.setFkOrderId(ztcOrder.getId());
				List<ZtcOrderTicket> tickets = ztcOrderTicketManager.searchInfoEquals(ztcOrderTicket);
				for (int j = 0; j < tickets.size(); j++) {
					ZtcOrderTicket orderTicket = tickets.get(j);
					if (orderTicket.getStatus() == 0) {
						if (orderTicket.getSeatType() == 3) {
							haveSaleChildSeatNumber++;
						}
					}
				}
			}
		}
		int seatInfoNumber = commonOnSeatNumber + specialOnSeatNumber + highOnSeatNumber + commonSaleSeatNumber
				+ specialSaleSeatNumber + highSaleSeatNumber;
		int calculateChildSeatNumber = ((seatInfoNumber) / 10);
		if (calculateChildSeatNumber - haveSaleChildSeatNumber < ztcClassesPriceForSale.getRemainChildSeatNumber()) {
			if (calculateChildSeatNumber - haveSaleChildSeatNumber > 0) {
				ztcClassesPriceForSale.setRemainChildSeatNumber(calculateChildSeatNumber - haveSaleChildSeatNumber);
			} else {
				ztcClassesPriceForSale.setRemainChildSeatNumber(0);
			}
		}
		if (modifySeatInfos.length() > 0) {
			modifySeatInfos = modifySeatInfos.substring(0, modifySeatInfos.length() - 1);
		}
		return modifySeatInfos;
	}

	/**
	 * 正则表达式效验参数
	 * @param arguments 要效验参数及其正则表达式规则集合
	 * @param wapper 用于装载效验结果
	 */
	private void examineArgument(HashMap<String, String> arguments, HashMap<String, Object> wapper) {
		for (String key : arguments.keySet()) {
			String[] keyCells = key.split("#");
			String[] valueCells = arguments.get(key).split("#");
			String element = keyCells[0];
			if (valueCells.length > 0) {
				for (int i = 0; i < valueCells.length; i++) {
					if (!element.matches(valueCells[i])) {
						wapper.put("success", Boolean.valueOf(false));
						wapper.put("msg", keyCells[i + 1]);
						return;
					}
				}
			}
		}
	}

	/**
	 * 检验座位信息是否合理
	 * @param data 梯形表数据
	 * @param wapper 用于装载效验结果信息
	 * @throws ParseException
	 */
	private void examineSeatInfo(String data, HashMap<String, Object> wapper) throws ParseException {
		LinkedHashMap<String, String> argumentsAndRoles = new LinkedHashMap<String, String>();
		JSONArray siteArray = new JSONArray();
		JSONArray priceArray = new JSONArray();
		JSONArray dataArray = JSONArray.parseArray(data);
		getSiteAndPriceArray(dataArray, siteArray, priceArray);
		for (int i = 0; i < priceArray.size(); i++) {
			JSONObject priceObject = priceArray.getJSONObject(i);
			String seatInfos = "";
			if (priceObject.getString("seat") != null && !"".equals(priceObject.getString("seat"))) {
				seatInfos = priceObject.getString("seat");
				if (!"".equals(seatInfos) && seatInfos != null) {
					JSONArray seatArray = JSONArray.parseArray(seatInfos);
					for (int j = 0; j < seatArray.size(); j++) {
						JSONObject seat = seatArray.getJSONObject(j);
						if (seat == null) {
							continue;
						}
						String price = seat.getString("pirce");
						if (!"".equals(price)) {
							argumentsAndRoles.clear();
							if (price.length() > 10) {
								wapper.put("success", Boolean.valueOf(false));
								wapper.put("msg", "价格的长度不可超过10位");
								return;
							}
							argumentsAndRoles.put(price + "#价格的格式如'100.01'",
									"^(([1-9][0-9]{0,9})|([0-9])|(([0-9].[0-9]{1,2})|([1-9][0-9]{1,7}.[0-9]{1})|([1-9][0-9]{1,6}.[0-9]{2})))$");
							examineArgument(argumentsAndRoles, wapper);
						}
					}
				}
			}
		}
	}

	/**
	 *
	 * 根据梯形表数据生成站点集合和价格集合
	 * @param dataArray 梯形表数据
	 * @param siteArray 站点集合
	 * @param priceArray 价格集合
	 */
	private void getSiteAndPriceArray(JSONArray dataArray, JSONArray siteArray, JSONArray priceArray) {
		if (dataArray != null) {
			for (int i = 0; i < dataArray.size(); i++) {
				JSONObject siteObject = dataArray.getJSONObject(i);
				String siteId = siteObject.getString("id");
				String siteName = siteObject.getString("name");
				String siteDetail = siteObject.getString("detailed");
				String siteLat = siteObject.getString("lat");
				String managerPhone = siteObject.getString("managerPhone");
				String siteDepartureTime = siteObject.getString("departureTime");
				String siteString = "{\"id\":\"" + siteId + "\",\"name\":\"" + siteDetail + "\",\"areaCity\":\""
						+ siteName + "\",\"lat\":\"" + siteLat + "\",\"managerPhone\":\"" + managerPhone
						+ "\",\"departureTime\":\"" + siteDepartureTime + "\",\"row\":\"" + i + "\"}";
				JSONArray price = siteObject.getJSONArray("cell");
				if (price != null) {
					for (int j = 0; j < price.size(); j++) {
						JSONObject object = price.getJSONObject(j);
						String priceId = object.getString("id");
						String priceValue = object.getString("pirce");
						String distance = object.getString("distance");
						String seat = object.getString("seat");
						seat = seat.replaceAll("\"", "'");
						String priceString = "{\"id\":\"" + priceId + "\",\"price\":\"" + priceValue
								+ "\",\"distance\":\"" + distance + "\",\"seat\":\"" + seat + "\",\"row\":\"" + i
								+ "\",\"col\":\"" + j + "\"}";
						JSONObject priceObject = JSON.parseObject(priceString);
						priceArray.add(priceObject);
					}
				}
				siteObject = JSON.parseObject(siteString);
				siteArray.add(siteObject);
			}
		}
	}

	/**
	 * 根据包含座位信息的在线班次价格对象，儿童座修改在线班次价格并生成修改后的座位信息
	 * @param objectPrice 包含座位信息的在线班次价格对象
	 * @param ztcClassesPriceForSale 需要修改的在线班次价格
	 * @param childSeatNumber 儿童座数
	 * @return 修改后的座位信息
	 */
	private String modifySeatInfo(JSONObject objectPrice, ZtcClassesPriceForSale ztcClassesPriceForSale,
								  String childSeatNumber) {
		String priceSeat = objectPrice.getString("seat");
		JSONArray seatArray = JSONArray.parseArray(priceSeat);
		String seatInfo = ztcClassesPriceForSale.getSeatInfo();
		if("".equals(seatInfo)||"()".equals(seatInfo)){
			return seatInfo;
		}
		String[] seatInfos = seatInfo.split("\\u0029,");
		String modifySeatInfos = "";
		int commonOnSeatNumber = 0;
		int specialOnSeatNumber = 0;
		int highOnSeatNumber = 0;
		int commonSaleSeatNumber = 0;
		int specialSaleSeatNumber = 0;
		int highSaleSeatNumber = 0;
		HashMap<String, Object> result = modifySeatInfoAssistant(seatInfos, seatArray, modifySeatInfos,
				ztcClassesPriceForSale, commonOnSeatNumber, specialOnSeatNumber, highOnSeatNumber, commonSaleSeatNumber,
				specialSaleSeatNumber, highSaleSeatNumber);
		modifySeatInfos = String.valueOf(result.get("modifySeatInfos"));
		commonOnSeatNumber = Integer.parseInt(String.valueOf(result.get("commonOnSeatNumber")));
		specialOnSeatNumber = Integer.parseInt(String.valueOf(result.get("specialOnSeatNumber")));
		highOnSeatNumber = Integer.parseInt(String.valueOf(result.get("highOnSeatNumber")));
		commonSaleSeatNumber = Integer.parseInt(String.valueOf(result.get("commonSaleSeatNumber")));
		specialSaleSeatNumber = Integer.parseInt(String.valueOf(result.get("specialSaleSeatNumber")));
		highSaleSeatNumber = Integer.parseInt(String.valueOf(result.get("highSaleSeatNumber")));
		int remainSeatNumber = commonOnSeatNumber + specialOnSeatNumber + highOnSeatNumber;
		if (remainSeatNumber == 0) {
			ztcClassesPriceForSale.setStatus(1);
		}
		ztcClassesPriceForSale.setRemainSeatNumber(remainSeatNumber);
		int haveSaleChildSeatNumber = 0;
		ZtcOrder order = new ZtcOrder();
		order.setFkZtcClassesPriceForSaleId(ztcClassesPriceForSale.getId());
		List<ZtcOrder> orders = ztcOrderManager.searchInfo(order);
		for (int i = 0; i < orders.size(); i++) {
			ZtcOrder ztcOrder = orders.get(i);
			if (ztcOrder.getStatus() == 0 || ztcOrder.getStatus() == 1) {
				ZtcOrderTicket ztcOrderTicket = new ZtcOrderTicket();
				ztcOrderTicket.setFkOrderId(ztcOrder.getId());
				List<ZtcOrderTicket> tickets = ztcOrderTicketManager.searchInfoEquals(ztcOrderTicket);
				for (int j = 0; j < tickets.size(); j++) {
					ZtcOrderTicket orderTicket = tickets.get(j);
					if (orderTicket.getStatus() == 0) {
						if (orderTicket.getSeatType() == 3) {
							haveSaleChildSeatNumber++;
						}
					}
				}
			}
		}
		int seatInfoNumber = commonOnSeatNumber + specialOnSeatNumber + highOnSeatNumber + commonSaleSeatNumber
				+ specialSaleSeatNumber + highSaleSeatNumber;
		int calculateChildSeatNumber = ((seatInfoNumber) / 10);
		if (calculateChildSeatNumber - haveSaleChildSeatNumber > Integer.parseInt(childSeatNumber)) {
			ztcClassesPriceForSale.setRemainChildSeatNumber(Integer.parseInt(childSeatNumber));
		} else {
			if (calculateChildSeatNumber - haveSaleChildSeatNumber > 0) {
				ztcClassesPriceForSale.setRemainChildSeatNumber(calculateChildSeatNumber - haveSaleChildSeatNumber);
			} else {
				ztcClassesPriceForSale.setRemainChildSeatNumber(0);
			}
		}
		if (modifySeatInfos.length() > 0) {
			modifySeatInfos = modifySeatInfos.substring(0, modifySeatInfos.length() - 1);
		}
		return modifySeatInfos;
	}

	/**
	 * 以在线班次价格座位信息，由梯形表获取的座位信息集合，在线班次价格生成座位信息，统计各种座位数
	 * @param seatInfos 在线班次座位信息
	 * @param seatArray  由梯形表获取的座位信息集合
	 * @param modifySeatInfos 装载生成的座位信息
	 * @param ztcClassesPriceForSale 在线班次
	 * @param commonOnSeatNumber  普通在售座位数目
	 * @param specialOnSeatNumber 特价在售座位数目
	 * @param highOnSeatNumber 高价在售座位数目
	 * @param commonSaleSeatNumber 普通已售座位数目
	 * @param specialSaleSeatNumber 特价已售座位数目
	 * @param highSaleSeatNumber 高价已售座位数目
	 * @return 包含新生成的座位信息，各种座位数的集合
	 */
	private HashMap<String, Object> modifySeatInfoAssistant(String[] seatInfos, JSONArray seatArray,
															String modifySeatInfos, ZtcClassesPriceForSale ztcClassesPriceForSale, int commonOnSeatNumber,
															int specialOnSeatNumber, int highOnSeatNumber, int commonSaleSeatNumber, int specialSaleSeatNumber,
															int highSaleSeatNumber) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		float priceValue = Float.parseFloat(ztcClassesPriceForSale.getPrice());
		for (int i = 0; i < seatInfos.length; i++) {
			String seat = seatInfos[i];
			if ("".equals(seat)) {
				continue;
			}
			int seatIndex = seat.indexOf(")");
			if (seatIndex > 0) {
				seat = seat.substring(1, seatIndex);
			} else {
				seat = seat.substring(1);
			}
			String[] tags = seat.split(",");
			int index = -1;
			for (int j = 0; j < seatArray.size(); j++) {
				JSONObject seatObject = seatArray.getJSONObject(j);
				String seatNumber = seatObject.getString("seatnumber");
				String state = seatObject.getString("state");
				float price = Float.parseFloat(seatObject.getString("pirce"));
				if (tags[0].equals(seatNumber)) {
					if ("0".equals(tags[1])) {
						if ("2".equals(state)) {
							//
						} else if ("0".equals(state)) {
							if (price < priceValue) {
								modifySeatInfos += "(" + seatNumber + ",0," + price + ",1),";
								specialOnSeatNumber++;
							} else if (price > priceValue) {
								modifySeatInfos += "(" + seatNumber + ",0," + price + ",2),";
								highOnSeatNumber++;
							} else {
								modifySeatInfos += "(" + seatNumber + ",0," + price + ",0),";
								commonOnSeatNumber++;
							}
						}
					} else if ("2".equals(tags[1])) {
						if ("0".equals(state)) {
							if (price < priceValue) {
								modifySeatInfos += "(" + seatNumber + ",0," + price + ",1),";
								specialOnSeatNumber++;
							} else if (price > priceValue) {
								modifySeatInfos += "(" + seatNumber + ",0," + price + ",2),";
								highOnSeatNumber++;
							} else {
								modifySeatInfos += "(" + seatNumber + ",0," + price + ",0),";
								commonOnSeatNumber++;
							}
						}
					} else {
						modifySeatInfos += "(" + seat + "),";
						if ("0".equals(tags[1])) {
							commonSaleSeatNumber++;
						} else if ("1".equals(tags[1])) {
							specialSaleSeatNumber++;
						} else {
							highSaleSeatNumber++;
						}
					}
					index = j;
					break;
				}
			}
			if (index > -1) {
				seatArray.remove(index);
			}
		}
		for (int i = 0; i < seatArray.size(); i++) {
			JSONObject seatObject = seatArray.getJSONObject(i);
			String seatNumber = seatObject.getString("seatnumber");
			String state = seatObject.getString("state");
			float price = Float.parseFloat(seatObject.getString("pirce"));
			if ("0".equals(state)) {
				if (price < priceValue) {
					modifySeatInfos += "(" + seatNumber + "," + state + "," + price + ",1),";
					specialOnSeatNumber++;
				} else if (price > priceValue) {
					modifySeatInfos += "(" + seatNumber + "," + state + "," + price + ",2),";
					highOnSeatNumber++;
				} else {
					modifySeatInfos += "(" + seatNumber + "," + state + "," + price + ",0),";
					commonOnSeatNumber++;
				}
			}
		}
		result.put("modifySeatInfos", modifySeatInfos);
		result.put("commonOnSeatNumber", commonOnSeatNumber);
		result.put("specialOnSeatNumber", specialOnSeatNumber);
		result.put("highOnSeatNumber", highOnSeatNumber);
		result.put("commonSaleSeatNumber", commonSaleSeatNumber);
		result.put("specialSaleSeatNumber", specialSaleSeatNumber);
		result.put("highSaleSeatNumber", highSaleSeatNumber);
		return result;
	}

	/**
	 * 根据页数，名称查找在线班次
	 * @param pageNumber 页数
	 * @param name 名称
	 * @param session
	 * @return 在线班次集合，在线班次集合大小，名称，页数
	 */
	@RequestMapping("getList")
	@ResponseBody
	public HashMap<String, Object> getList(String pageNumber, String name, HttpSession session) {
		HashMap<String, Object> wapper = new HashMap<>();
		LinkedHashMap<String, String> argumentsAndRoles = new LinkedHashMap<>();
		if (pageNumber != null) {
			argumentsAndRoles.put(pageNumber + "#页数必须为数字", "^(([0-9])|([1-9][0-9]{0,}))$");
			examineArgument(argumentsAndRoles, wapper);
			if ("false".equals(String.valueOf(wapper.get("success")))) {
				return wapper;
			}
		}
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
		ZtcClassesForSale classesForSale = new ZtcClassesForSale();
		classesForSale.setName(name);
		classesForSale.setFkTransportId(user.getTransportId());
		List<ZtcClassesForSale> list = ztcClassesForSaleManager.searchInfoLikes(classesForSale,
				String.valueOf(transport.getType()), pageNumber);
		for (int i = 0; i < list.size(); i++) {
			ZtcClassesForSale classesForSaleBean = list.get(i);
			ZtcClassesPriceForSale price = new ZtcClassesPriceForSale();
			price.setFkClassesForSaleId(classesForSaleBean.getId());
			List<ZtcClassesPriceForSale> prices = ztcClassesPriceForSaleManager.searchInfoEquals(price);
			int remainSeatNumber = 0;
			int remainChildSeatNumber = 0;
			int ticketNumber = 0;
			for (int j = 0; j < prices.size(); j++) {
				ZtcClassesPriceForSale priceForSale = prices.get(j);
				remainSeatNumber += priceForSale.getRemainSeatNumber();
				remainChildSeatNumber += priceForSale.getRemainChildSeatNumber();
				String seatInfo = priceForSale.getSeatInfo();
				String[] seatInfos = seatInfo.split("\\u0029,");
				for (int k = 0; k < seatInfos.length; k++) {
					String seat = seatInfos[k];
					if ("".equals(seat)) {
						continue;
					}
					int seatIndex = seat.indexOf(")");
					if (seatIndex > 0) {
						seat = seat.substring(1, seatIndex);
					} else {
						seat = seat.substring(1);
					}
					String[] tags = seat.split(",");
					if ("1".equals(tags[1])) {
						ticketNumber++;
					}
				}
			}
			classesForSaleBean.setRemainSeatNumber(remainSeatNumber);
			classesForSaleBean.setRemainChildSeatNumber(remainChildSeatNumber);
			classesForSaleBean.setTicketNumber(ticketNumber);
		}
		String stringList = generateList(list);
		JSONArray jsonList = JSON.parseArray(stringList);
		wapper.put("list", jsonList);
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("listSize",
				ztcClassesForSaleManager.getCount(name, String.valueOf(transport.getType()), user.getTransportId()));
		wapper.put("name", name);
		wapper.put("pageNumber", pageNumber);
		return wapper;
	}

	/**
	 * 根据班次模板id获取其所有的在线班次
	 * @param classesId 班次模板id
	 * @param session
	 * @return 可供页面展示的由在线班次集合生成的信息
	 */
	@RequestMapping("searchList")
	@ResponseBody
	public HashMap<String, Object> searchList(String classesId, HttpSession session) {
		HashMap<String, Object> wapper = new HashMap<>();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
		int transportType = transport.getType();
		if (transportType == 0 || transportType == 1 || transportType == 2) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "非运营公司或车方不可操作在售班次");
			log.info("非运营公司或车方不可操作在售班次");
			return wapper;
		}
		ZtcClassesForSale classesForSale = new ZtcClassesForSale();
		classesForSale.setFkZtcClassesId(classesId);
		List<ZtcClassesForSale> list = ztcClassesForSaleManager.searchInfoEquals(classesForSale);
		String stringList = generateList(list);
		JSONArray jsonList = JSON.parseArray(stringList);
		wapper.put("list", jsonList);
		wapper.put("success", Boolean.valueOf(true));
		return wapper;
	}

	/**
	 * 根据在线班次集合生成可供页面展示的信息
	 * @param list 在线班次集合
	 * @return 可供页面展示的信息
	 */
	private String generateList(List<ZtcClassesForSale> list) {
		String stringList = "[";
		for (int i = 0; i < list.size(); i++) {
			ZtcClassesForSale ztcClassesForSale = list.get(i);
			String element = "{\"name\":\"" + ztcClassesForSale.getName() + "\",\"remark\":\""
					+ ztcClassesForSale.getRemark() + "\",\"addTime\":\"" + ztcClassesForSale.getAddTime()
					+ "\",\"departureDate\":\"" + DATE_FORMET.format(ztcClassesForSale.getDepartureDate())
					+ "\",\"ticketStatus\":\"" + ztcClassesForSale.getTicketStatus();
			if (ztcClassesForSale.getFkZtcCarId() == null) {
				element += "\",\"carNumber\":\"" + "随机";
			} else {
				ZtcCar car = ztcCarManager.get(ztcClassesForSale.getFkZtcCarId());
				if (car != null) {
					element += "\",\"carNumber\":\"" + car.getCarNumber();
				} else {
					element += "\",\"carNumber\":\"" + "随机";
				}
			}
			if (ztcClassesForSale.getFkZtcDriverId() == null) {
				element += "\",\"driverPhone\":\"" + "调度";
			} else {
				ZtcDriver driver = ztcDriverManager.get(ztcClassesForSale.getFkZtcDriverId());
				if (driver != null) {
					element += "\",\"driverPhone\":\"" + driver.getPhone();
				} else {
					element += "\",\"driverPhone\":\"" + "调度";
				}
			}
			element += "\",\"remainSeatNumber\":\"" + ztcClassesForSale.getRemainSeatNumber();
			element += "\",\"remainChildSeatNumber\":\"" + ztcClassesForSale.getRemainChildSeatNumber();
			element += "\",\"ticketNumber\":\"" + ztcClassesForSale.getTicketNumber();
			ZtcClassesSiteForSale siteExample = new ZtcClassesSiteForSale();
			siteExample.setFkZtcClassesForSaleId(ztcClassesForSale.getId());
			List<ZtcClassesSiteForSale> sites = ztcClassesSiteForSaleManager.searchInfoEquals(siteExample);
			ZtcClassesSiteForSale startSite = sites.get(0);
			ZtcClassesSiteForSale endSite = sites.get(sites.size() - 1);
			element += "\",\"startSiteName\":\"" + startSite.getAreaCity() + "\",\"startSiteAreaCity\":\""
					+ startSite.getName() + "\",\"startSiteManagerPhone\":\"" + startSite.getManagerPhone()
					+ "\",\"startSiteLat\":\"" + startSite.getLat() + "\",\"startSiteRow\":\"" + startSite.getSequence()
					+ "\",\"startSiteDepartureTime\":\"" + startSite.getDepartureTime() + "\",\"endSiteName\":\""
					+ endSite.getAreaCity() + "\",\"endSiteAreaCity\":\"" + endSite.getName()
					+ "\",\"endSiteManagerPhone\":\"" + endSite.getManagerPhone() + "\",\"endSiteLat\":\""
					+ endSite.getLat() + "\",\"endSiteRow\":\"" + endSite.getSequence() + "\",\"endSiteDepartureTime\":\""
					+ endSite.getDepartureTime();
			element += "\",\"id\":\"" + ztcClassesForSale.getId();
			element += "\",\"cell\":[";
			for (int j = 0; j < sites.size(); j++) {
				ZtcClassesSiteForSale classesSiteForSale = sites.get(j);
				if (j == 0) {
					element += "{\"name\":\"" + classesSiteForSale.getAreaCity()
							+ "\",\"remark\":\"起点\",\"sequence\":\"" + classesSiteForSale.getSequence()
							+ "\",\"areaCity\":\"" + classesSiteForSale.getName() + "\",\"managerPhone\":\""
							+ classesSiteForSale.getManagerPhone() + "\",\"lat\":\"" + classesSiteForSale.getLat()
							+ "\",\"row\":\"" + classesSiteForSale.getSequence()
							+ "\",\"id\":\"" + classesSiteForSale.getId()
							+ "\",\"type\":\"" + classesSiteForSale.getType()
							+ "\",\"departureTime\":\""
							+ classesSiteForSale.getDepartureTime() + "\",\"areaCode\":\""
							+ classesSiteForSale.getAreaCode() + "\",\"lat\":\"" + classesSiteForSale.getLat() + "\"},";
				} else if (j == sites.size() - 1) {
					element += "{\"name\":\"" + classesSiteForSale.getAreaCity()
							+ "\",\"remark\":\"终点\",\"sequence\":\"" + classesSiteForSale.getSequence()
							+ "\",\"areaCity\":\"" + classesSiteForSale.getName() + "\",\"managerPhone\":\""
							+ classesSiteForSale.getManagerPhone() + "\",\"lat\":\"" + classesSiteForSale.getLat()
							+ "\",\"row\":\"" + classesSiteForSale.getSequence()
							+ "\",\"id\":\"" + classesSiteForSale.getId()
							+ "\",\"type\":\"" + classesSiteForSale.getType()
							+ "\",\"departureTime\":\""
							+ classesSiteForSale.getDepartureTime() + "\",\"areaCode\":\""
							+ classesSiteForSale.getAreaCode() + "\",\"lat\":\"" + classesSiteForSale.getLat() + "\"}";
				} else {
					element += "{\"name\":\"" + classesSiteForSale.getAreaCity() + "\",\"sequence\":\""
							+ classesSiteForSale.getSequence() + "\",\"areaCity\":\"" + classesSiteForSale.getName()
							+ "\",\"managerPhone\":\"" + classesSiteForSale.getManagerPhone() + "\",\"lat\":\""
							+ classesSiteForSale.getLat() + "\",\"row\":\"" + classesSiteForSale.getSequence()
							+ "\",\"id\":\"" + classesSiteForSale.getId()
							+ "\",\"type\":\"" + classesSiteForSale.getType()
							+ "\",\"departureTime\":\""
							+ classesSiteForSale.getDepartureTime() + "\",\"areaCode\":\""
							+ classesSiteForSale.getAreaCode() + "\",\"lat\":\"" + classesSiteForSale.getLat() + "\"},";
				}
			}
			element += "]}";
			stringList += element;
			if (i != list.size() - 1) {
				stringList += ",";
			}
		}
		stringList += "]";
		return stringList;
	}

	/**
	 * 根据班次模板id查找从当前时间到当前时间35天的在线班次，并生成可供页面展示的在线班次信息
	 * @param classesId 班次模板id
	 * @param session
	 * @return 可供页面展示的信息
	 * @throws ParseException
	 */
	@RequestMapping("findList")
	@ResponseBody
	public HashMap<String, Object> findList(String classesId, HttpSession session) throws ParseException {
		HashMap<String, Object> wapper = new HashMap<>();
		Date now=new Date();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcClasses ztcClasses = new ZtcClasses();
		ztcClasses.setId(classesId);
		ZtcClasses classes = ztcClassesManager.searchInfoEquals(ztcClasses).get(0);
		ZtcClassesForSale classesForSale = new ZtcClassesForSale();
		classesForSale.setFkZtcClassesId(classesId);
		Date startDate = classes.getDepartureDate();
		if(now.getTime()>startDate.getTime()){
			startDate=now;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(startDate);
		calendar.add(Calendar.DATE, 35);
		Date endDate = calendar.getTime();
		List<ZtcClassesForSale> list = ztcClassesForSaleManager.searchInfoWithDate(classesForSale,
				DATE_FORMET.format(startDate), DATE_FORMET.format(endDate));
		String stringList = generateNewList(list, classes);
		JSONArray jsonList = JSON.parseArray(stringList);
		wapper.put("list", jsonList);
		wapper.put("preSaleDays", classes.getPreSaleDays());
		wapper.put("success", Boolean.valueOf(true));
		return wapper;
	}

	/**
	 *
	 * 根据在线班次集合，班次模板生成可供页面展示的在线班次信息
	 * @param list 在线班次集合
	 * @param classes 班次模板
	 * @return  可供页面展示的在线班次信息
	 * @throws ParseException
	 */
	@SuppressWarnings("static-access")
	private String generateNewList(List<ZtcClassesForSale> list, ZtcClasses classes) throws ParseException {
		int preSaleDays = classes.getPreSaleDays();
		List<Date> arrangeDates = new ArrayList<>();
		Date nowDate = new Date();
		Date startDate=classes.getDepartureDate();
		Calendar calandar = Calendar.getInstance();
		calandar.setTime(nowDate.getTime()>startDate.getTime()?nowDate:startDate);
		arrangeDates.add(calandar.getTime());
		for (int i = 1; i < 35; i++) {
			calandar.add(Calendar.DATE, 1);
			arrangeDates.add(calandar.getTime());
		}
		Calendar calandarOne = Calendar.getInstance();
		calandar.setTime(DATE_TIME_FORMET.parse(DATE_FORMET.format(nowDate)+" "+"00:00:00"));
		calandarOne.add(calandar.DATE, preSaleDays-1);
		Date preSaleEndDate = calandarOne.getTime();
		String stringList = "[";
		for (int i = 0; i < arrangeDates.size(); i++) {
			Date selectDate = arrangeDates.get(i);
			Calendar calendarTwo = Calendar.getInstance();
			calendarTwo.setTime(selectDate);
			int dd = calendarTwo.get(Calendar.DAY_OF_MONTH);
			int month = calendarTwo.get(Calendar.MONTH) + 1;
			int weekOfMonth = calendarTwo.get(Calendar.DAY_OF_WEEK) - 1;
			String dateString = DATE_FORMET.format(selectDate);
			selectDate = DATE_FORMET.parse(dateString);
			int count = 0;
			int index = -1;
			for (int j = 0; j < list.size(); j++) {
				ZtcClassesForSale ztcClassesForSale = list.get(j);
				Date departureDate = ztcClassesForSale.getDepartureDate();
				if (departureDate.getTime() == selectDate.getTime()) {
					if (ztcClassesForSale.getTicketStatus() == 1) {
						stringList += "{\"data\":\"" + DATE_FORMET.format(departureDate) + "\",\"dd\":\"" + dd
								+ "\",\"Month\":\"" + month + "\",\"id\":\"" + ztcClassesForSale.getId()
								+ "\",\"classesId\":\"" + classes.getId()
								+ "\",\"week\":\"" + weekOfMonth + "\",\"state\":\"" + "3\"";
					} else {
						if (departureDate.getTime() < preSaleEndDate.getTime()) {
							stringList += "{\"data\":\"" + DATE_FORMET.format(departureDate) + "\",\"dd\":\"" + dd
									+ "\",\"Month\":\"" + month + "\",\"id\":\"" + ztcClassesForSale.getId()
									+ "\",\"classesId\":\"" + classes.getId()
									+ "\",\"week\":\"" + weekOfMonth + "\",\"state\":\"" + "1\"";
						} else {
							stringList += "{\"data\":\"" + DATE_FORMET.format(departureDate) + "\",\"dd\":\"" + dd
									+ "\",\"Month\":\"" + month + "\",\"id\":\"" + ztcClassesForSale.getId()
									+ "\",\"classesId\":\"" + classes.getId()
									+ "\",\"week\":\"" + weekOfMonth + "\",\"state\":\"" + "0\"";
						}
					}
					stringList += "},";
				} else {
					count++;
				}
			}
			if (count == list.size()) {
				// 拼凑当天数据
				stringList += "{\"data\":\"" + DATE_FORMET.format(selectDate) + "\",\"dd\":\"" + dd + "\",\"Month\":\""+ month
						+ "\",\"classesId\":\"" + classes.getId()
						+ "\",\"week\":\"" + weekOfMonth + "\",\"state\":\"" + "2\"";
				stringList += "},";
			} else {
				if (index > 0) {
					list.remove(index);
				}
			}
		}
		if (!"[".equals(stringList)) {
			stringList.substring(0, stringList.length() - 1);
		}
		stringList += "]";
		return stringList;
	}


	/**
	 * 根据在线班次id，发车日期生成梯形表数据
	 * @param id 在线班次id
	 * @param departureDate 发车日期
	 * @param session
	 * @return 包含在线班次id，梯形表数据的集合
	 * @throws ParseException
	 */
	@RequestMapping("getForModify")
	@ResponseBody
	public HashMap<String, Object> getForModify(String id, String departureDate, HttpSession session)
			throws ParseException {
		HashMap<String, Object> wapper = new HashMap<>();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		if(id!=null){
			ZtcClassesForSale ztcClassesForSale = new ZtcClassesForSale();
			ztcClassesForSale.setId(id);
			ztcClassesForSale.setDepartureDate(DATE_FORMET.parse(departureDate));
			ZtcClassesForSale ztcClassesForSaleBean = ztcClassesForSaleManager.searchInfoEquals(ztcClassesForSale).get(0);
			List<ZtcClassesSiteForSale> classesSiteForSales = ztcClassesForSaleManager
					.getSitesForSale(ztcClassesForSaleBean);
			List<ZtcClassesPriceForSale> classesPriceForSales = ztcClassesForSaleManager
					.getPricesForSale(ztcClassesForSaleBean);
			String data = generateDataForModify(classesSiteForSales, classesPriceForSales);
			wapper.put("success", Boolean.valueOf(true));
			wapper.put("id", ztcClassesForSaleBean.getId());
			wapper.put("data", JSON.parseArray(data));
		}else{
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "网络错误");
			log.info("网络错误");
			return wapper;
		}
		return wapper;
	}

	/**
	 * 根据在线班次站点集合，在线班次价格集合 生成梯形表数据
	 * @param classesSiteForSales 在线班次站点集合
	 * @param classesPriceForSales 在线班次价格集合
	 * @return 梯形表数据
	 */
	private String generateDataForModify(List<ZtcClassesSiteForSale> classesSiteForSales,
										 List<ZtcClassesPriceForSale> classesPriceForSales) {
		String data = "[";
		List<ZtcClassesSiteForSale> startZtcClassesSitesForSale = new ArrayList<ZtcClassesSiteForSale>();
		String startName = "";
		for (int i = 0; i < classesSiteForSales.size(); i++) {
			ZtcClassesSiteForSale endZctClassesSiteForSale = classesSiteForSales.get(i);
			/*if (i == 0) {
				startName = (endZctClassesSiteForSale.getAreaCity() == null ? "" : endZctClassesSiteForSale.getAreaCity());
			}*/
			String cell = "[";
			for (int j = 0; j < startZtcClassesSitesForSale.size(); j++) {
				ZtcClassesSiteForSale startZtcClassesSiteForSale = startZtcClassesSitesForSale.get(j);
				if (classesSiteForSales.size() == 0) {
					cell += "{\"pirce\":\"\",\"distance\":\"\",\"seat\":[]}";
					if (j != startZtcClassesSitesForSale.size() - 1) {
						cell += ",";
					}
				} else {
					boolean findResult = false;
					int index = 0;
					startName = (startZtcClassesSiteForSale.getAreaCity() == null ? "" : startZtcClassesSiteForSale.getAreaCity());
					for (int k = 0; k < classesPriceForSales.size(); k++) {
						ZtcClassesPriceForSale classesPrice = classesPriceForSales.get(k);
						if (startZtcClassesSiteForSale.getId().equals(classesPrice.getFkStartClassesSiteForSaleId())
								&& endZctClassesSiteForSale.getId()
								.equals(classesPrice.getFkEndClassesSiteForSaleId())) {
							index = k;
							cell += "{\"pirce\":\"" + classesPrice.getPrice() + "\",\"id\":\"" + classesPrice.getId()
									+ "\",\"distance\":\"" + classesPrice.getDistance() + "\",\"startName\":\""
									+ startName + "\"}";
							if (j != startZtcClassesSitesForSale.size() - 1) {
								cell += ",";
							}
							findResult = true;
							break;
						}
					}
					if (findResult) {
						classesPriceForSales.remove(index);
					} else {
						cell += "{\"pirce\":\"\",\"distance\":\"\"}";
						if (j != startZtcClassesSitesForSale.size() - 1) {
							cell += ",";
						}
					}
				}
			}
			if (!"[".equals(cell)) {
				cell.substring(0, cell.length() - 1);
			}
			cell += "]";
			data = generateCellContentInfo(classesSiteForSales, data, i, endZctClassesSiteForSale, cell);
			startZtcClassesSitesForSale.add(endZctClassesSiteForSale);
		}
		if (!"[".equals(data)) {
			data.substring(0, data.length() - 1);
		}
		data += "]";
		return data;
	}

	/**
	 * 获取在售班次信息
	 * @param id 在售班次id
	 * @param session
	 * @return 操作结果信息
	 */
	@RequestMapping("get")
	@ResponseBody
	public HashMap<String, Object> get(String id, HttpSession session) {
		HashMap<String, Object> wapper = new HashMap<>();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcClassesForSale ztcClassesForSale = new ZtcClassesForSale();
		ztcClassesForSale.setId(id);
		ZtcClassesForSale ztcClassesForSaleBean = ztcClassesForSaleManager.searchInfoEquals(ztcClassesForSale).get(0);
		List<ZtcClassesSiteForSale> classesSiteForSales = ztcClassesForSaleManager
				.getSitesForSale(ztcClassesForSaleBean);
		List<ZtcClassesPriceForSale> classesPriceForSales = ztcClassesForSaleManager
				.getPricesForSale(ztcClassesForSaleBean);
		List<ZtcRouteAttachment> routeAttachments = ztcClassesForSaleManager.getAttachments(ztcClassesForSaleBean);
		String data = generateData(ztcClassesForSaleBean,classesSiteForSales, classesPriceForSales);
		String attachment = generateAttachment(routeAttachments);
		ZtcCar car = ztcCarManager.get(ztcClassesForSaleBean.getFkZtcCarId());
		ZtcDriver driver = ztcDriverManager.get(ztcClassesForSaleBean.getFkZtcDriverId());
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("carType", ztcClassesForSaleBean.getCarType());
		wapper.put("seatNumber", ztcClassesForSaleBean.getSeatNumber());
		wapper.put("childSeatNumber", ztcClassesForSaleBean.getChildSeatNumber());
		wapper.put("forbidSaleTimeRegionBeforeDeparture",
				ztcClassesForSaleBean.getForbidSaleTimeRegionBeforeDeparture());
		wapper.put("name", ztcClassesForSaleBean.getName());
		wapper.put("remark", ztcClassesForSaleBean.getRemark());
		wapper.put("isRefund", ztcClassesForSaleBean.getIsRefund());
		wapper.put("id", ztcClassesForSaleBean.getId());
		wapper.put("car", car);
		wapper.put("driver", driver);
		wapper.put("data", JSON.parseArray(data));
		wapper.put("attachment", JSON.parseArray(attachment));
		return wapper;
	}

	/**
	 * 根据在线班次对象，在线班次站点集合，在线班次价格集合生成梯形表数据
	 * @param ztcClassesForSaleBean 在线班次对象
	 * @param classesSiteForSales 在线班次站点集合
	 * @param classesPriceForSales 在线班次价格集合
	 * @return 梯形表数据
	 */
	private String generateData(ZtcClassesForSale ztcClassesForSaleBean,List<ZtcClassesSiteForSale> classesSiteForSales,
								List<ZtcClassesPriceForSale> classesPriceForSales) {
		String data = "[";
		List<ZtcClassesSiteForSale> startZtcClassesSitesForSale = new ArrayList<ZtcClassesSiteForSale>();
		String startName = "";
		for (int i = 0; i < classesSiteForSales.size(); i++) {
			ZtcClassesSiteForSale endZctClassesSiteForSale = classesSiteForSales.get(i);
		/*	if (i == 0) {
				startName = (endZctClassesSiteForSale.getAreaCity() == null ? "" : endZctClassesSiteForSale.getAreaCity());
			}*/
			String cell = "[";
			for (int j = 0; j < startZtcClassesSitesForSale.size(); j++) {
				ZtcClassesSiteForSale startZtcClassesSiteForSale = startZtcClassesSitesForSale.get(j);
				if (classesSiteForSales.size() == 0) {
					cell += "{\"pirce\":\"\",\"distance\":\"\",\"seat\":[]}";
					if (j != startZtcClassesSitesForSale.size() - 1) {
						cell += ",";
					}
				} else {
					boolean findResult = false;
					int index = 0;
					startName=(startZtcClassesSiteForSale.getAreaCity() == null ? "" : startZtcClassesSiteForSale.getAreaCity());
					for (int k = 0; k < classesPriceForSales.size(); k++) {
						ZtcClassesPriceForSale classesPrice = classesPriceForSales.get(k);
						if (startZtcClassesSiteForSale.getId().equals(classesPrice.getFkStartClassesSiteForSaleId())
								&& endZctClassesSiteForSale.getId()
								.equals(classesPrice.getFkEndClassesSiteForSaleId())) {
							index = k;
							cell += "{\"pirce\":\"" + classesPrice.getPrice() + "\",\"id\":\"" + classesPrice.getId()
									+ "\",\"distance\":\"" + classesPrice.getDistance() + "\",\"startName\":\""
									+ startName + "\",\"seat\":" + JSON.parseArray(generateSeatInfo(classesPrice))
									+ "}";
							if (j != startZtcClassesSitesForSale.size() - 1) {
								cell += ",";
							}
							findResult = true;
							break;
						}
					}
					if (findResult) {
						classesPriceForSales.remove(index);
					} else {
						cell += "{\"pirce\":\"\",\"distance\":\"\",\"seat\":[]}";
						if (j != startZtcClassesSitesForSale.size() - 1) {
							cell += ",";
						}
					}
				}
			}
			if (!"[".equals(cell)) {
				cell.substring(0, cell.length() - 1);
			}
			cell += "]";
			data = generateCellContentInfo(classesSiteForSales, data, i, endZctClassesSiteForSale, cell);
			startZtcClassesSitesForSale.add(endZctClassesSiteForSale);
		}
		if (!"[".equals(data)) {
			data.substring(0, data.length() - 1);
		}
		data += "]";
		return data;
	}

	/**
	 * 根据在线班次价格生成座位信息
	 * @param classesPrice 在线班次价格
	 * @return 座位信息
	 */
	private String generateSeatInfo(ZtcClassesPriceForSale classesPrice) {
		String info = "[";
		String seatInfo = classesPrice.getSeatInfo();
		if("".equals(seatInfo)||"()".equals(seatInfo)){
			return seatInfo;
		}
		if (!"".equals(seatInfo) && seatInfo != null) {
			String[] seatInfos = seatInfo.split("\\u0029,");
			for (int i = 0; i < seatInfos.length; i++) {
				String seat = seatInfos[i];
				if ("".equals(seat)) {
					continue;
				}
				int seatIndex = seat.indexOf(")");
				if (seatIndex > 0) {
					seat = seat.substring(1, seatIndex);
				} else {
					seat = seat.substring(1);
				}
				String[] tags = seat.split(",");
				info += "{" + "'pirce':'" + tags[2] + "','seatnumber':'" + tags[0] + "','state':'" + tags[1]
						+ "','checkTrue':false},";
			}
			info = info.substring(0, info.length() - 1);
			info += "]";
		} else {
			info += "]";
		}
		return info;
	}

	/**
	 * 根据在线班次站点集合，当前在线班次站点下标，在线班次结束站点，价格信息生成梯形表的一行数据
	 * @param classesSiteForSales 在线班次站点集合
	 * @param data 用于装载梯形表一行数据
	 * @param i 当前在线班次站点下标
	 * @param endZctClassesSiteForSale 在线班次结束站点
	 * @param cell 价格信息
	 * @return 梯形表的一行数据
	 */
	private String generateCellContentInfo(List<ZtcClassesSiteForSale> classesSiteForSales, String data, int i,
										   ZtcClassesSiteForSale endZctClassesSiteForSale, String cell) {
		if (i == 0) {
			data += "{\"name\":\""
					+ (endZctClassesSiteForSale.getAreaCity() == null ? "" : endZctClassesSiteForSale.getAreaCity())
					+ "\",\"id\":\"" + endZctClassesSiteForSale.getId()
					+ "\",\"type\":\"" + endZctClassesSiteForSale.getType()
					+ "\",\"managerPhone\":\""
					+ endZctClassesSiteForSale.getManagerPhone() + "\",\"lat\":\"" + endZctClassesSiteForSale.getLat()
					+ "\",\"row\":\"" + endZctClassesSiteForSale.getSequence() + "\",\"departureTime\":\""
					+ (endZctClassesSiteForSale.getDepartureTime() == null ? ""
					: DATE_TIME_FORMET.format(endZctClassesSiteForSale.getDepartureTime()))
					+ "\",\"departureDate\":\""
					+ (endZctClassesSiteForSale.getDepartureTime() == null ? ""
					: DATE_TIME_FORMET.format(endZctClassesSiteForSale.getDepartureTime()))
					+ "\",\"remark\":\"起点\",\"detailed\":\""
					+ (endZctClassesSiteForSale.getName() == null ? "" : endZctClassesSiteForSale.getName()) + "\"},";
		} else if (i == classesSiteForSales.size() - 1) {
			data += "{\"name\":\""
					+ (endZctClassesSiteForSale.getAreaCity() == null ? "" : endZctClassesSiteForSale.getAreaCity())
					+ "\",\"id\":\"" + endZctClassesSiteForSale.getId()
					+ "\",\"type\":\"" + endZctClassesSiteForSale.getType()
					+ "\",\"managerPhone\":\""
					+ endZctClassesSiteForSale.getManagerPhone() + "\",\"lat\":\"" + endZctClassesSiteForSale.getLat()
					+ "\",\"row\":\"" + endZctClassesSiteForSale.getSequence() + "\",\"departureTime\":\""
					+ (endZctClassesSiteForSale.getDepartureTime() == null ? ""
					: DATE_TIME_FORMET.format(endZctClassesSiteForSale.getDepartureTime()))
					+ "\",\"departureDate\":\""
					+ (endZctClassesSiteForSale.getDepartureTime() == null ? ""
					: DATE_TIME_FORMET.format(endZctClassesSiteForSale.getDepartureTime()))
					+ "\",\"remark\":\"终点\",\"detailed\":\""
					+ (endZctClassesSiteForSale.getName() == null ? "" : endZctClassesSiteForSale.getName())
					+ "\",\"cell\":" + cell + "},";
		} else {
			data += "{\"name\":\""
					+ (endZctClassesSiteForSale.getAreaCity() == null ? "" : endZctClassesSiteForSale.getAreaCity())
					+ "\",\"id\":\"" + endZctClassesSiteForSale.getId()
					+ "\",\"type\":\"" + endZctClassesSiteForSale.getType()
					+ "\",\"managerPhone\":\""
					+ endZctClassesSiteForSale.getManagerPhone() + "\",\"lat\":\"" + endZctClassesSiteForSale.getLat()
					+ "\",\"row\":\"" + endZctClassesSiteForSale.getSequence() + "\",\"departureTime\":\""
					+ (endZctClassesSiteForSale.getDepartureTime() == null ? ""
					: DATE_TIME_FORMET.format(endZctClassesSiteForSale.getDepartureTime()))
					+ "\",\"departureDate\":\""
					+ (endZctClassesSiteForSale.getDepartureTime() == null ? ""
					: DATE_TIME_FORMET.format(endZctClassesSiteForSale.getDepartureTime()))
					+ "\",\"detailed\":\""
					+ (endZctClassesSiteForSale.getName()== null ? "" : endZctClassesSiteForSale.getName())
					+ "\",\"cell\":" + cell + "},";
		}
		return data;
	}

	/**
	 * 根据线路附件集合生成可供页面展示的附件信息
	 * @param routeAttachments 线路附件集合
	 * @return 可供页面展示的附件信息
	 */
	private String generateAttachment(List<ZtcRouteAttachment> routeAttachments) {
		int groupIndex = 0;
		int groupNumber = 0;
		if (routeAttachments.size() > 0) {
			groupNumber = routeAttachments.get(0).getGroupNumber();
		}
		int count = 0;
		String attachmentValue = "[";
		while (true) {
			String attachmentElement = "{\"group\":[";
			for (int i = groupIndex; i < routeAttachments.size(); i++) {
				ZtcRouteAttachment routeAttachment = routeAttachments.get(i);
				if (routeAttachment.getGroupNumber() != groupNumber) {
					groupIndex = i;
					groupNumber = routeAttachment.getGroupNumber();
					break;
				}
				if (routeAttachment.getFkAttachmentId() == null || "".equals(routeAttachment.getFkAttachmentId())) {
					attachmentElement += "{\"img\":\"\",\"text\":\"" + routeAttachment.getViewContent() + "\"}";
				} else {
					Attachment attachment = routeAttachment.getAttachment();
					attachmentElement += "{\"img\":\"data:" + attachment.getFileType() + ";base64,"
							+ getFileBody(attachment.getFileBody()) + "\",\"text\":\""
							+ routeAttachment.getViewContent() + "\"}";
				}
				count++;
			}
			if (count == routeAttachments.size()) {
				attachmentElement += "]}";
				attachmentValue += attachmentElement;
				break;
			} else {
				attachmentElement += "]},";
				attachmentValue += attachmentElement;
			}
		}
		attachmentValue += "]";
		return attachmentValue;
	}

	/**
	 * 解密base64位
	 * @param fileBody 附件base64位图片数据
	 * @return 解密后的base64位图片数据
	 */
	private String getFileBody(byte[] fileBody) {
		for (int j = 0; j < fileBody.length; ++j) {
			if (fileBody[j] > 0) {
				// 调整异常数据
				fileBody[j] -= 256;
			}
		}
		String bodyInfo = new BASE64Encoder().encode(fileBody);
		return bodyInfo;
	}
/*	*//**
	 * 获取班次模板可统一修改的部分信息，包括车型，车，司机，停售时间，预售期，是否可退票，总座数，儿童座
	 * @param id 班次模板id
	 * @param session
	 * @return 操作结果信息
	 *//*
	@RequestMapping("getToUpdateSomeInfo")
	@ResponseBody
	public HashMap<String, Object> getToUpdateSomeInfo(String id, HttpSession session) {
		User user = (User) session.getAttribute("userInfo");
		HashMap<String, Object> wapper = new HashMap<>();
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcClassesForSale ztcClassesForSale = new ZtcClassesForSale();
		ztcClassesForSale.setId(id);
		ZtcClassesForSale ztcClassesForSaleBean = ztcClassesForSaleManager.searchInfoEquals(ztcClassesForSale).get(0);
		ZtcCar car = ztcCarManager.get(ztcClassesForSale.getFkZtcCarId());
		ZtcDriver driver = ztcDriverManager.get(ztcClassesForSale.getFkZtcDriverId());
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("id", id);
		wapper.put("carType", ztcClassesForSale.getCarType());
		wapper.put("car", car);
		wapper.put("driver", driver);
		wapper.put("seatNumber", ztcClassesForSale.getSeatNumber());
		wapper.put("childSeatNumber", ztcClassesForSale.getChildSeatNumber());
		wapper.put("isRefund", ztcClassesForSale.getIsRefund());
		wapper.put("forbidSaleTimeRegionBeforeDeparture", ztcClassesForSale.getForbidSaleTimeRegionBeforeDeparture());
		return wapper;
	}

	*//**
	 * 修改班次模板及其当前日期之后所有在线班次的部分信息
	 * @param id 在线班次模板id
	 * @param carType 车型
	 * @param carId 车辆id
	 * @param driverId 司机id
	 * @param seatNumber 总座位数
	 * @param childSeatNumber 儿童座位数
	 * @param isRefund 是否可退票：true，是；false，否
	 * @param fsTimerbDeparture 开车前多长时间停止售票
	 * @param session
	 * @return 操作结果信息
	 * @throws ParseException 字符串转日期出错
	 *//*
	@RequestMapping("updateSomeInfo")
	@ResponseBody
	public HashMap<String, Object> updateSomeInfo(String id, String carType, String carId, String driverId, String seatNumber,
												  String childSeatNumber, String isRefund, String fsTimerbDeparture, HttpSession session) throws ParseException {
		Date now = new Date();
		HashMap<String, Object> wapper = new HashMap<>();
		User user = (User) session.getAttribute("userInfo");
		if (user == null || "".equals(user)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "用户未登录");
			log.info("用户未登录");
			return wapper;
		}
		ZtcTransport transport = ztcTransportManager.get(user.getTransportId());
		int transportType = transport.getType();
		if (transportType == 0 || transportType == 1 || transportType == 2) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "非运营公司或车方不可操作班次");
			log.info("非运营公司或车方不可操作班次");
			return wapper;
		}
		if (transport.getStatus() == 4) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该用户公司已被冻结，不可修改班次");
			log.info("该用户公司已被冻结，不可修改班次");
			return wapper;
		}
		LinkedHashMap<String, String> argumentsAndRoles = new LinkedHashMap<>();
		if ("".equals(carId) || carId == null) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "请选择车辆");
			return wapper;
		}
		if ("".equals(driverId) || driverId == null) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "请选择司机");
			return wapper;
		}
		if ("".equals(fsTimerbDeparture) || fsTimerbDeparture == null) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "请填写发车前暂停售票的时间范围");
			return wapper;
		}
		argumentsAndRoles.put(fsTimerbDeparture + "#发车前暂停售票的时间范围格式如'1.01'",
				"^(([1-9][0-9]{0,9})|([0])|(([0-9].[0-9]{1,2})|([1-9][0-9]{1,7}.[0-9]{1})|([1-9][0-9]{1,6}.[0-9]{2})))$");
		if ("".equals(seatNumber) || seatNumber == null) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "请填写总座数");
			return wapper;
		}
		argumentsAndRoles.put(seatNumber + "#总票格式如'9'或'12'最小为1最大不超过65", "^(([1-9])|([1-5][0-9])|([6][0-5]))$");
		if ("".equals(childSeatNumber) || childSeatNumber == null) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "请填写儿童座数");
			return wapper;
		}
		argumentsAndRoles.put(childSeatNumber + "#儿童票格式如'0'最大不超过6", "^[0-6]$");
		examineArgument(argumentsAndRoles, wapper);
		if ("false".equals(String.valueOf(wapper.get("success")))) {
			return wapper;
		}
		// 修改魔板班次
		ZtcClasses ztcClasses = new ZtcClasses();
		ztcClasses.setId(id);
		ZtcClasses ztcClassesBean = ztcClassesManager.searchInfoEquals(ztcClasses).get(0);
		if (ztcClassesBean.getTicketStatus() == 1) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "该班次已停售");
			log.info("该班次已停售");
			return wapper;
		}
		int intSeatNumber = Integer.parseInt(seatNumber);
		int intChildSeatNumber = Integer.parseInt(childSeatNumber);
		if (intChildSeatNumber > (int) (intSeatNumber * childSeatRatio)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "儿童票不能高于成人票的10%");
			log.info("儿童票不能高于成人票的10%");
			return wapper;
		}
		ZtcClassesForSale ztcClassesForSale = new ZtcClassesForSale();
		ztcClassesForSale.setId(id);
		ZtcClassesForSale ztcClassesForSaleBean = ztcClassesForSaleManager.searchInfoEquals(ztcClassesForSale).get(0);
		// 修改在售班次价格信息
		ZtcClassesPriceForSale price = new ZtcClassesPriceForSale();
		price.setFkClassesForSaleId(id);
		List<ZtcClassesPriceForSale> prices = ztcClassesPriceForSaleManager.searchInfoEquals(price);
		int stopPriceCount = 0;
		int onPriceCount = 0;
		for (int i = 0; i < prices.size(); i++) {
			// 需要修改价格和距离
			ZtcClassesPriceForSale priceForSale = prices.get(i);
			ZtcOrder ztcOrder = new ZtcOrder();
			ztcOrder.setFkZtcClassesPriceForSaleId(priceForSale.getId());
			int saleChildSeatNumber = 0;
			List<ZtcOrder> orders = orderManager.searchInfo(ztcOrder);
			List<ZtcOrderTicket> orderTickets = new ArrayList<ZtcOrderTicket>();
			if (orders != null && orders.size() > 0) {
				for (int j = 0; j < orders.size(); j++) {
					ZtcOrder order = orders.get(j);
					ZtcOrderTicket ticket = new ZtcOrderTicket();
					ticket.setFkOrderId(order.getId());
					ticket.setStatus(0);
					orderTickets.addAll(orderTicketManager.searchInfoEquals(ticket));
				}
			}
			if (orderTickets.size() > 0) {
				for (int j = 0; j < orderTickets.size(); j++) {
					ZtcOrderTicket ticket = orderTickets.get(j);
					if (ticket.getSeatType() == 3) {
						saleChildSeatNumber++;
					}
				}
			}
			String seatInfo = priceForSale.getSeatInfo();
			String[] seatInfos = seatInfo.split("\\u0029,");
			int[] seatNumbers = new int[seatInfos.length];
			for (int j = 0; j < seatInfos.length; j++) {
				String seat = seatInfos[j];
				if ("".equals(seat)) {
					continue;
				}
				int seatIndex = seat.indexOf(")");
				if (seatIndex > 0) {
					seat = seat.substring(1, seatIndex);
				} else {
					seat = seat.substring(1);
				}
				String[] tags = seat.split(",");
				seatNumbers[j] = Integer.parseInt(tags[0]);
			}
			Arrays.sort(seatNumbers);
			if (Integer.parseInt(seatNumber) < seatNumbers.length) {
				// 减少总座位数，删掉多余座位信息，重设儿童座位数
				stopPriceCount = subClassesPriceForSaleSeatInfo(seatNumber, childSeatNumber, priceForSale,
						saleChildSeatNumber, seatInfos, seatNumbers);
			} else {
				// 添加总座位数,添加少的座位信息，重设儿童座位数
				onPriceCount = addClassesPriceForSaleSeatInfo(seatNumber, childSeatNumber, priceForSale,
						saleChildSeatNumber, seatInfos, seatNumbers);
			}
			ztcClassesPriceForSaleManager.update(priceForSale);
		}
		ztcClassesForSaleBean.setCarType(Integer.parseInt(carType));
		ztcClassesForSaleBean.setFkZtcCarId(carId);
		ztcClassesForSaleBean.setFkZtcDriverId(driverId);
		ztcClassesForSaleBean.setIsRefund("true".equals(isRefund) ? 1 : 0);
		ztcClassesForSaleBean.setForbidSaleTimeRegionBeforeDeparture(fsTimerbDeparture);
		ztcClassesForSaleBean.setChildSeatNumber(Integer.parseInt(childSeatNumber));
		ztcClassesForSaleBean.setSeatNumber(Integer.parseInt(seatNumber));
		ztcClassesForSaleBean.setModifyTime(now);
		ztcClassesForSaleBean.setFkModifyUserId(user == null ? "" : user.getId());
		if (stopPriceCount == prices.size()) {
			ztcClassesForSaleBean.setTicketStatus(1);
		} else {
			if (onPriceCount > 0) {
				ztcClassesForSaleBean.setTicketStatus(0);
			}
		}
		String result = ztcClassesForSaleManager.update(ztcClassesForSaleBean);
		if ("0".equals(result)) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("msg", "网络错误");
			log.info("网络错误");
			return wapper;
		}
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", "修改成功");
		log.info("修改成功");
		return wapper;
	}
	*//**
	 * 减少总座位数，删掉多余座位信息，重设儿童座位数
	 * @param seatNumber 总座数
	 * @param childSeatNumber 儿童座位数
	 * @param priceForSale 在线班次价格对象
	 * @param saleChildSeatNumber 已售儿童座位
	 * @param seatInfos 座位信息
	 * @param seatNumbers  座位号集合
	 * @return 停售的在线班次数量
	 *//*
	private int subClassesPriceForSaleSeatInfo(String seatNumber, String childSeatNumber,
											   ZtcClassesPriceForSale priceForSale, int saleChildSeatNumber, String[] seatInfos, int[] seatNumbers) {
		int stopPriceCount = 0;
		int subSeatNumber = seatNumbers.length - Integer.parseInt(seatNumber);
		String newSeatInfo = "";
		int commonSaleSeatNumber = 0;
		int specialSaleSeatNumber = 0;
		int highSaleSeatNumber = 0;
		int commonOnSeatNumber = 0;
		int specialOnSeatNumber = 0;
		int highOnSeatNumber = 0;
		for (int l = 0; l < seatInfos.length; l++) {
			String seat = seatInfos[l];
			if ("".equals(seat)) {
				continue;
			}
			int seatIndex = seat.indexOf(")");
			if (seatIndex > 0) {
				seat = seat.substring(1, seatIndex);
			} else {
				seat = seat.substring(1);
			}
			String[] tags = seat.split(",");
			if (tags[1].equals("1")) {
				newSeatInfo += "(" + seat + "),";
				if ("0".equals(tags[3])) {
					commonSaleSeatNumber++;
				} else if ("1".equals(tags[3])) {
					specialSaleSeatNumber++;
				} else {
					highSaleSeatNumber++;
				}
			} else if (tags[1].equals("2")) {
				// newSeatInfo += "(" + seat + "),";
			} else {
				if (subSeatNumber > 0) {
					subSeatNumber--;
				} else {
					newSeatInfo += "(" + tags[0] + "," + tags[1] + "," + priceForSale.getPrice() + "," + tags[3] + "),";
					if ("0".equals(tags[3])) {
						commonOnSeatNumber++;
					} else if ("1".equals(tags[3])) {
						specialOnSeatNumber++;
					} else {
						highOnSeatNumber++;
					}
				}
			}
		}
		if (!"".equals(newSeatInfo)) {
			newSeatInfo = newSeatInfo.substring(0, newSeatInfo.length() - 1);
		}
		// 计算剩余儿童座位数
		int seatInfoNumber = commonSaleSeatNumber + specialSaleSeatNumber + highSaleSeatNumber + commonOnSeatNumber
				+ specialOnSeatNumber + highOnSeatNumber;
		int calculateChildSeatNumber = (int) ((seatInfoNumber) * childSeatRatio);
		if (calculateChildSeatNumber - saleChildSeatNumber < Integer.parseInt(childSeatNumber)) {
			if (calculateChildSeatNumber - saleChildSeatNumber > 0) {
				priceForSale.setRemainChildSeatNumber(calculateChildSeatNumber - saleChildSeatNumber);
			} else {
				priceForSale.setRemainChildSeatNumber(0);
			}
		} else {
			priceForSale.setRemainChildSeatNumber(Integer.parseInt(childSeatNumber));
		}
		priceForSale.setSeatInfo(newSeatInfo);
		int remainSeatNumber = commonOnSeatNumber + specialOnSeatNumber + highOnSeatNumber;
		priceForSale.setRemainSeatNumber(remainSeatNumber);
		if (remainSeatNumber <= 0) {
			priceForSale.setStatus(1);
			stopPriceCount++;
		}
		return stopPriceCount;
	}

	*//**
	 * 添加总座位数,添加少的座位信息，重设儿童座位数
	 * @param seatNumber 总座数
	 * @param childSeatNumber 儿童座位数
	 * @param priceForSale 在线班次价格对象
	 * @param saleChildSeatNumber 已售的儿童票座位数
	 * @param seatInfos 座位信息
	 * @param seatNumbers 座位号集合
	 * @return 在售的在线班次价格数
	 *//*
	private int addClassesPriceForSaleSeatInfo(String seatNumber, String childSeatNumber,
											   ZtcClassesPriceForSale priceForSale, int saleChildSeatNumber, String[] seatInfos, int[] seatNumbers) {
		int onPriceCount = 0;
		String newSeatInfo = "";
		int actualAddSeatNumber = Integer.parseInt(seatNumber) - seatNumbers.length;
		int orignalAddSeatNumber = actualAddSeatNumber;
		int onSeatNumber = 0;
		int recoverySeatNumber = 0;
		int saleSeatNumber = 0;
		for (int l = 0; l < seatInfos.length; l++) {
			String seat = seatInfos[l];
			if ("".equals(seat)) {
				continue;
			}
			int seatIndex = seat.indexOf(")");
			if (seatIndex > 0) {
				seat = seat.substring(1, seatIndex);
			} else {
				seat = seat.substring(1);
			}
			String[] tags = seat.split(",");
			if ("0".equals(tags[1])) {
				newSeatInfo += "(" + tags[0] + "," + tags[1] + "," + priceForSale.getPrice() + "," + tags[3] + "),";
				onSeatNumber++;
			} else if ("2".equals(tags[1])) {
				if (recoverySeatNumber < actualAddSeatNumber) {
					newSeatInfo += "(" + tags[0] + ",0," + priceForSale.getPrice() + "," + tags[3] + "),";
					recoverySeatNumber++;
				}
			} else if ("1".equals(tags[1])) {
				newSeatInfo += "(" + tags[0] + "," + tags[1] + "," + priceForSale.getPrice() + "," + tags[3] + "),";
				saleSeatNumber++;
			}
		}
		actualAddSeatNumber = actualAddSeatNumber - recoverySeatNumber;
		if (actualAddSeatNumber > 0) {
			int firstNumber = seatNumbers[0];
			if (firstNumber > 1) {
				for (int l = 1; l < firstNumber; l++) {
					if (actualAddSeatNumber > 0) {
						newSeatInfo += "(" + l + ",0," + priceForSale.getPrice() + ",0),";
						actualAddSeatNumber--;
					} else {
						break;
					}
				}
			}
			if (actualAddSeatNumber > 0) {
				for (int l = 0; l < seatNumbers.length - 1; l++) {
					int start = seatNumbers[l];
					int end = seatNumbers[l + 1];
					for (int m = start + 1; m < end; m++) {
						if (actualAddSeatNumber > 0) {
							newSeatInfo += "(" + m + ",0," + priceForSale.getPrice() + ",0),";
							actualAddSeatNumber--;
						} else {
							break;
						}
					}
					if (actualAddSeatNumber <= 0) {
						break;
					}
				}
			}
			if (actualAddSeatNumber > 0) {
				int lastNumber = seatNumbers[seatNumbers.length - 1];
				for (int l = lastNumber + 1; l < 66; l++) {
					if (actualAddSeatNumber > 0) {
						newSeatInfo += "(" + l + ",0," + priceForSale.getPrice() + ",0),";
						actualAddSeatNumber--;
					} else {
						break;
					}
				}
			}
		}
		if (!"".equals(newSeatInfo)) {
			newSeatInfo = newSeatInfo.substring(0, newSeatInfo.length() - 1);
		}
		// 计算剩余儿童座位数
		int seatInfoNumber = onSeatNumber + recoverySeatNumber + saleSeatNumber + orignalAddSeatNumber
				- actualAddSeatNumber;
		int calculateChildSeatNumber = (int) (seatInfoNumber * childSeatRatio);
		if (calculateChildSeatNumber - saleChildSeatNumber < Integer.parseInt(childSeatNumber)) {
			if (calculateChildSeatNumber - saleChildSeatNumber > 0) {
				priceForSale.setRemainChildSeatNumber(calculateChildSeatNumber - saleChildSeatNumber);
			} else {
				priceForSale.setRemainChildSeatNumber(0);
			}
		} else {
			priceForSale.setRemainChildSeatNumber(Integer.parseInt(childSeatNumber));
		}
		priceForSale.setSeatInfo(newSeatInfo);
		int remainSeatNumber = onSeatNumber + recoverySeatNumber + orignalAddSeatNumber - actualAddSeatNumber;
		if (priceForSale.getRemainSeatNumber() < remainSeatNumber) {
			priceForSale.setStatus(0);
			onPriceCount++;
		}
		priceForSale.setRemainSeatNumber(remainSeatNumber);
		return onPriceCount;
	}*/
}